英文:
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))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论