Google oauth2 端点不返回用户个人资料信息(姓名等)

标签 go oauth-2.0 google-oauth userinfo

我正在尝试在我的网络服务 (golang) 上使用 google oauth2,但无法获取用户个人资料信息(名字、姓氏)。

我正在尝试各种端点,但每次都会得到这样的答案:

{
    "id":"*************",
    "email":"***@gmail.com",
    "verified_email":true,
    "picture":"https://***/photo.jpg"
}

同时使用这组范围的 google playground 返回这种答案:

{
  "family_name": "***", 
  "name": "****", 
  "picture": "https://*******/photo.jpg", 
  "locale": "ru", 
  "email": "****@gmail.com", 
  "given_name": "*****", 
  "id": "************", 
  "verified_email": true
}

如何在我的网络应用程序中获得相同的答案? 此站点和互联网上的人建议添加配置文件范围,但我已经这样做了。

这是我的代码(缩短)

"golang.org/x/oauth2"
"golang.org/x/oauth2/google"

func init() {
    googleOauthConfig = &oauth2.Config{
        RedirectURL:  "*******",
        ClientID:     "*********",
        ClientSecret: "******",
        Scopes: []string{
            "https://www.googleapis.com/auth/userinfo.email",
            "https://www.googleapis.com/auth/userinfo.profile",
            "openid"},
        Endpoint: google.Endpoint,
    }
}

var googleOauthConfig *oauth2.Config

//const oauthGoogleURLAPI = "https://www.googleapis.com/oauth2/v1/userinfo"
const oauthGoogleURLAPI = "https://www.googleapis.com/oauth2/v2/userinfo"
//const oauthGoogleURLAPI = "https://www.googleapis.com/oauth2/v3/userinfo"
const oauthGoogleURLAPI2 = "https://people.googleapis.com/v1/people/me?personFields=names,emailAddresses"
//const oauthGoogleURLAPI = "https://openidconnect.googleapis.com/v1/userinfo"

func HandleGoogleCallback(w http.ResponseWriter, r *http.Request) {
    ...

    oauthState, _ := r.Cookie(oauthstateCookieName)

    code := r.FormValue("code") 

    gtoken, err := googleOauthConfig.Exchange(oauth2.NoContext, code)

    bearer := "Bearer " + gtoken.AccessToken

    req, err := http.NewRequest("GET", oauthGoogleURLAPI, nil)
    req.Header.Add("Authorization", bearer)

    gresponse, err := http.DefaultClient.Do(req)

    contents, err := ioutil.ReadAll(gresponse.Body)

    authContent := &AuthContent{}

    err = json.Unmarshal(contents, authContent)

    log.Println(authContent)

    ...
}

我也读过这篇文章Google Oauth2 userinfo API not returning user's name data 但结果是一样的:没有个人资料信息。

谢谢。

更新: 根据评论的建议,我稍微更正了“golang.org/x/oauth2”的代码并获取了 id_token。 当我在 jwt.io 上查看这个 id token 时,我看到了我需要的所有数据。 但我认为个人资料信息应该通过这样的 API 请求来检索

req, err := http.NewRequest("GET", <endPoint>, nil)
gresponse, err := http.DefaultClient.Do(req)

在这个例子中只使用了标准的 go 库并且 gresponse 包含了整个 http 响应,不是吗?为什么响应不包含我需要的字段?

最佳答案

  1. 处理 googleOauthConfig.Exchange(oauth2.NoContext, code) 中的错误
  2. 处理 http.DefaultClient.Do 中的错误
  3. 处理 ioutil.ReadAll(gresponse.Body) 中的错误
  4. 处理 json.Unmarshal 中的错误

你可能会找到答案。

我还记得我总是发送 access_token 作为查询参数:

response, err := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken)

关于Google oauth2 端点不返回用户个人资料信息(姓名等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56538832/

相关文章:

c - 与 C 相比,为什么 Go 向文件写入字节很慢

java - protoc-gen-java代码使用Inline对象提示语法错误

go - Sqlite 并发写入性能

asp.net - 如何在 Asp.Net Web API 2 中使用 Owin OAuth2 修改 token 端点响应正文

python - 如何使用 flask_oauthlib 交换访问 token 的一次性授权码?

gmail - Google OAuth发送邮件的范围

api - Go 不会立即响应 GET 请求,它会在 ticker 完成后响应

oauth - 为什么 OAuth2 授权代码授予流程中的授权请求不需要客户端 key ?

oauth-2.0 - Auth0 JWT 访问 token

android - 如何从 Google Play 获取用户下载的应用程序(付费/免费)列表?