如何正确使用Golang中的OAuth2获取google电子邮件

如何正确使用Golang中的OAuth2获取google电子邮件,go,oauth-2.0,google-oauth,Go,Oauth 2.0,Google Oauth,我已经尝试在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\u代码 auth_code := ctx.Request.URL.Raw

我已经尝试在
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\u代码

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)
我在
回复
(正文为空)中找不到任何说明电子邮件部分的内容:

第一个问题,如何正确获取电子邮件?没有使用Google+API

编辑#2我尝试使用另一个
作用域
oauth2.Config

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

编辑#3我尝试使用另一个作用域:

openid
profile
email
并尝试使用以下URL检索电子邮件:

https://www.googleapis.com/oauth2/v3/userinfo
但它仍然像以前一样给出空的
正文


第二个问题,我是否可以为其他用户重复使用
oauth2.Config
provider
)变量?或者我应该为每个用户创建一个副本吗?

我的错,我应该首先阅读
响应。正文,例如:

response, err = client.Get(`https://accounts.google.com/.well-known/openid-configuration`)
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
// 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`]
根据文档,我们应该首先从该URL获取,然后从上述结果中的
userinfo\u endpoint
获取电子邮件,例如:

response, err = client.Get(`https://accounts.google.com/.well-known/openid-configuration`)
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
// 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
struct是可重用的