I want to list my repositories on GitHub. Using curl, I am able to do this:
curl -u USR:TOKEN https://api.github.com/user/repos
When I translate this code to Go:
func FetchRepos(usr, token string) []Repository {
req, reqErr := http.NewRequest("Get", "https://api.github.com/user/repos", nil)
if reqErr != nil {
panic(reqErr)
}
req.SetBasicAuth(usr, token)
resp, respErr := http.DefaultClient.Do(req)
if respErr != nil {
panic(respErr)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// Decode json response
var data []Repository
json.Unmarshal(body, &data)
return data
}
I get an error printing out the resp
variable gives me a 403 Forbidden
.
I don't know what I'm doing wrong in this instance.
I have tried adding req.Header.Add("User-Agent", "request")
but this did not have any effect.