让Golang接受一个用于GET授权的令牌。

huangapple go评论86阅读模式
英文:

Get Golang to accept a token for GET authorization

问题

我似乎无法让我的脚本接受我的令牌。这是我的脚本:

func main(){

    url := "https://www.mywebsite.com"
    response, err := http.Get(url)   
    if err != nil {
        log.Fatalln(err)
        fmt.Print(err.Error())
        os.Exit(1)
    }

    response.Header.Add("Authorization", os.ExpandEnv("$BEARER_TOKEN"))

    fmt.Println(response.Header.Get("authorization"))


    responseData, err := ioutil.ReadAll(response.Body)
    if err != nil {
        log.Fatalln(err)
    }

    fmt.Println(string(responseData))
}

我使用fmt.Println(response.Header.Get("authorization"))来检查是否接受了令牌,并且它正确地打印了令牌,所以我不确定为什么它在GET请求中不接受它。

我还尝试过使用response.Header.Set而不是response.Header.Add,但结果相同。

当我运行该脚本时,我收到的响应是:{"code":16,"message":"token not valid for running mode"},但我知道令牌是正确的,因为我在curl命令中尝试过,并且得到了我想要的输出。

英文:

I cant seem to get my script to accept my bearer token. Here is my script:

func main(){

    url := "https://www.mywebsite.com"
	response, err := http.Get(url)   
    if err != nil {
        log.Fatalln(err)
		fmt.Print(err.Error())
		os.Exit(1)
	}

    response.Header.Add("Authorization", os.ExpandEnv("$BEARER_TOKEN"))

    fmt.Println(response.Header.Get("authorization"))


	responseData, err := ioutil.ReadAll(response.Body)
    if err != nil {
        log.Fatalln(err)
	}

	fmt.Println(string(responseData))
}

I did fmt.Println(response.Header.Get("authorization")) to see if it was accepting the token and it printed the token correctly, so im not sure why its not accepting it for the GET request.

Ive also tried response.Header.Set instead of response.Header.Add and it had the same outcome.

The response I get back when I run that script is:
{"code":16,"message":"token not valid for running mode"} but I know the token is correct because I tried it in a curl command and I got the output that I wanted

答案1

得分: 3

尝试使用http.NewRequest()并设置request的Header Authorization。

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {

	url := "https://www.mywebsite.com"

	client := http.Client{Timeout: 5 * time.Second}
	request, err := http.NewRequest(http.MethodGet, url, nil)

	request.Header.Set("Authorization", os.ExpandEnv("$BEARER_TOKEN"))
	request.Header.Set("Content-Type", "application/json") // => your content-type

	response, err := client.Do(request)
	if err != nil {
		panic(err)
	}

	responseData, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(string(responseData))
}
英文:

try to use http.NewRequest() and set request Header Authorization

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {

	url := "https://www.mywebsite.com"

	client := http.Client{Timeout: 5 * time.Second}
	request, err := http.NewRequest(http.MethodGet, url, nil)

	request.Header.Set("Authorization", os.ExpandEnv("$BEARER_TOKEN"))
	request.Header.Set("Content-Type", "application/json") // => your content-type

	response, err := client.Do(request)
	if err != nil {
		panic(err)
	}

	responseData, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(string(responseData))
}

huangapple
  • 本文由 发表于 2022年1月13日 19:56:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/70696189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定