gpt4 book ai didi

go - 如何在 Golang 中使用 OAuth2 正确获取谷歌电子邮件

转载 作者:IT王子 更新时间:2023-10-29 01:02:56 26 4
gpt4 key购买 nike

我已经尝试通过 golang.com/x/oauth2 库成功地使用 OAuth 进行身份验证。

// provider variable is oauth2.Config
// scope is: https://www.googleapis.com/auth/userinfo.email
url := provider.AuthCodeURL(``) // redirect URL

从客户端重定向回来后,我成功发送了auth_code

auth_code := ctx.Request.URL.RawQuery // code=XXXX
if len(auth_code) > 5 {
auth_code = auth_code[5:] // XXXX
}
tok, err := provider.Exchange(oauth2.NoContext, auth_code)
if err == nil {
client := provider.Client(oauth2.NoContext, tok)
email_url := `https://www.googleapis.com/auth/userinfo.email`
//Log.Describe(client)
response, err := client.Get(email_url)
if err == nil {
ctx.Render(`login_oauth`, response)
//handled = true
}
}
//Log.Describe(err)

我在 response 上找不到任何关于电子邮件部分的信息(Body 是空的):

{
"Status": "200 OK",
"StatusCode": 200,
"Proto": "HTTP/1.1",
"ProtoMajor": 1,
"ProtoMinor": 1,
"Header": {
"Alternate-Protocol": [
"443:quic,p=0.5"
],
"Cache-Control": [
"private, max-age=0"
],
"Content-Type": [
"text/plain"
],
"Date": [
"Tue, 14 Apr 2015 05:52:17 GMT"
],
"Expires": [
"Tue, 14 Apr 2015 05:52:17 GMT"
],
"Server": [
"GSE"
],
"X-Content-Type-Options": [
"nosniff"
],
"X-Frame-Options": [
"SAMEORIGIN"
],
"X-Xss-Protection": [
"1; mode=block"
]
},
"Body": {}, // empty!!!
"ContentLength": -1,
"TransferEncoding": [
"chunked"
],
"Close": false,
"Trailer": null,
"Request": {
"Method": "GET",
"URL": {
"Scheme": "https",
"Opaque": "",
"User": null,
"Host": "www.googleapis.com",
"Path": "/auth/userinfo.email",
"RawQuery": "",
"Fragment": ""
},
"Proto": "HTTP/1.1",
"ProtoMajor": 1,
"ProtoMinor": 1,
"Header": {
"Authorization": [
"Bearer ya29.VQFRHDe21t7g2cUhN8sUwjpRRi10XldgLe0RFhMe2ZxgyRo7q90HoKES5WmcucwKqtjZdq_KvYjKiQ"
]
},
"Body": null,
"ContentLength": 0,
"TransferEncoding": null,
"Close": false,
"Host": "www.googleapis.com",
"Form": null,
"PostForm": null,
"MultipartForm": null,
"Trailer": null,
"RemoteAddr": "",
"RequestURI": "",
"TLS": null
},
"TLS": {
// really long output
}
}

第一个问题,如何正确获取邮件?不使用 Google+ API。

edit #2 我试过为 oauth2.Config 使用另一个 scope:

https://www.googleapis.com/auth/plus.profile.emails.read
https://www.googleapis.com/auth/plus.login
https://www.googleapis.com/auth/plus.me

并尝试使用较新的 API 检索电子邮件:

https://www.googleapis.com/plus/v1/people/me

但它给出了 403 Forbidden

编辑 #3 我试过使用另一个范围:

openid
profile
email

并尝试使用此 URL 检索电子邮件:

https://www.googleapis.com/oauth2/v3/userinfo

但它仍然像以前一样给出空的 Body

第二个问题,我可以为另一个用户重用oauth2.Config (provider) 变量吗?还是应该为每个用户创建一个副本?

最佳答案

我的错,我应该先阅读response.Body,例如:

response, err = client.Get(`https://accounts.google.com/.well-known/openid-configuration`)
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()

根据 this文档,我们应该先从该 URL 中获取,然后从上面的结果中的 userinfo_endpoint 中获取以检索电子邮件,例如:

// json := json_to_map(body)
// get json[`userinfo_endpoint`]
// response, err = client.Get(json[`userinfo_endpoint`])
// body, err := ioutil.ReadAll(response.Body)
// response.Body.Close()
// json = json_to_map(body)
// json[`email`]

对于第二个问题,oauth2.Config 结构是可重用的。

关于go - 如何在 Golang 中使用 OAuth2 正确获取谷歌电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29620344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com