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

Google oauth2 endpoint does not return user profile info (name etc)

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

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

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

同时 google playground 使用这组范围 returns 这种答案:

{
  "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 令牌时,我看到了我需要的所有数据。 但我认为应该通过 API 这样的请求来检索个人资料信息

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

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

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

你可能会找到答案。

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

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

我找到了答案。那是我的错。 接收结果的结构看起来像这样

type AuthContent struct {
    ID            string `json:"id"`
    Email         string `json:"email"`
    VerifiedEmail bool   `json:"verified_email"`
    Picture       string `json:"picture"`
}

这就是响应中没有名称等字段的原因。