Not able to pass Bearer token in headers of a GET request in Golang

202 views
Skip to first unread message

Tahir Rauf

unread,
Oct 31, 2016, 8:35:00 AM10/31/16
to golang-nuts

I am using oauth2 to access a third party API. I can get the access token alright, but when I try to call the API by passing the bearer token in the request headers it gives me 401 (Unauthorized) error.  Although it works well when I try to do it via POSTMAN by passing headers as  (Authorization: Bearer <ACCESS_TOKE>). But it does not work using go.


Here is the code sample.


        url := "http://api.kounta.com/v1/companies/me.json"


var bearer = "Bearer " + <ACCESS TOKEN HERE>


req, err := http.NewRequest("GET", url, nil)

req.Header.Add("authorization", bearer)


client := urlfetch.Client(context)


resp, err := client.Do(req)

if err != nil {

panic(err)

}

defer resp.Body.Close()


body, _ := ioutil.ReadAll(resp.Body)

writer.Write([]byte(body)) // Gives 401 Unauthorized error, though same works using POSTMAN

Christian Joergensen

unread,
Oct 31, 2016, 9:00:08 AM10/31/16
to golang-nuts
Hi,

Have you tried inspecting the network traffic? Does the request contain the extra header?

What's urlfetch?

Cheers,
Reply all
Reply to author
Forward
0 new messages