【发布时间】:2015-02-21 22:12:28
【问题描述】:
我正在为 google oauth2 https://github.com/golang/oauth2 使用以下库
我正在使用示例中给出的代码(网址:http://play.golang.org/p/qXyuaVEhyS,https://godoc.org/golang.org/x/oauth2/google)
我能够获取授权码和令牌,但无法发出获取请求以获取用户信息
我的代码:
conf := &oauth2.Config{
ClientID: "my client id",
ClientSecret: "secred id",
RedirectURL: "http://localhost:3000/googlelogin",
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.profile",
},
Endpoint: google.Endpoint,
}
m.Get("/googleloginrequest", func(r render.Render, request *http.Request) {
url := conf.AuthCodeURL("state")
fmt.Printf("Visit the URL for the auth dialog: %v", url)
r.Redirect(url)
})
m.Get("/googlelogin", func(r render.Render, request *http.Request) {
authcode := request.FormValue("code")
tok, err := conf.Exchange(oauth2.NoContext, authcode)
if err != nil {
log.Fatal(err)
}
client := conf.Client(oauth2.NoContext, tok)
resp, err :=client.Get("https://www.googleapis.com/userinfo/v2/me")
r.JSON(200, map[string]interface{}{"status":resp})
})
我在这里得到的回复非常大并且没有任何用户信息
回复:
200 OK","StatusCode":200,"Proto":"HTTP/1.1","ProtoMajor":1,"ProtoMinor":1,"Header":{"Alternate-
Protocol":["443:quic,p=0.02"],"Cache-Control":["no-cache, no-store, max-age=0, must-
revalidate"],"Content-Type":["application/json; charset=UTF-8"],"Date":["Tue, 23 Dec 2014 18:18:19
GMT"],"Expires":["Fri, 01 Jan 1990 00:00:00 GMT"],"Pragma":["no-cache"],"Server":["GSE"],"Vary":
["Origin","X-Origin"],"X-Content-Type-Options":["nosniff"],"X-Frame-Options":["SAMEORIGIN"],"X-Xss-
Protection":["1; mode=block"]},"Body":{},"ContentLength":-1,"TransferEncoding":
["chunked"],"Close":false,"Trailer":null,"Request":{"Method":"GET","URL":
{"Scheme":"https","Opaque":"","User":null,"Host":"www.googleapis.com","Path":"/userinfo/v2/me","RawQuery
":"","Fragment":""},"Proto":"HTTP/1.1","ProtoMajor":1,"ProtoMinor":1,"Header":{"Authorization":["Bearer
ya29.5QDPWWRKB7tNkdB2Yvm0PCST9LF_iQhjN1Y0g2abE-
lnw9BNgEd_n3A85ZfJzDNYZywqqElCb7Z2xA"]},"Body":null,"ContentLength":0,"TransferEncoding":null,"Close":fa
lse,"Host":"www.googleapis.com","Form":null,"PostForm":null,"MultipartForm":null,"Trailer":null,"RemoteA
ddr":"","RequestURI":"","TLS":null},"TLS":{"Version":771,"HandshakeComplete":true,"DidResume":false,
....
请帮助或建议我在上述情况下工作的其他工作库/代码
【问题讨论】:
-
我遇到了类似的问题,顺便说一句,API 将在 2015 年 4 月 20 日被弃用..
-
嗨,我有一个使用另一个库的工作代码。请检查这篇文章的答案。如果您有任何疑问,请告诉我。
-
谢谢,我也找到了我的问题的解决方案stackoverflow.com/questions/29620344
标签: oauth go google-oauth martini