【问题标题】:Google OAuth2 API v2 userinfo always returns null for emailGoogle OAuth2 API v2 userinfo 始终为电子邮件返回 null
【发布时间】:2017-07-26 08:48:33
【问题描述】:

我正在尝试获取经过身份验证的用户的电子邮件地址。

我已经使用范围完成了身份验证

email
profile
https://www.googleapis.com/auth/plus.login
https://www.googleapis.com/plus/v1/people/me
https://www.googleapis.com/auth/userinfo.email

此调用为电子邮件返回 null,但填充了名称、链接、图片等:

Userinfoplus userinfo2 = oauth2.userinfo().v2().me().get().execute();
log.info(userinfo2.toString());

输出:

{
  "family_name" : "Homlish",
  "gender" : "male",
  "given_name" : "Paul",
  "id" : "107004799409225320539",
  "link" : "https://plus.google.com/107004799409225320539",
  "locale" : "en",
  "name" : "Paul Homlish",
  "picture" : "https://lh4.googleusercontent.com/-bCRlXUqr__E/AAAAAAAAAAI/AAAAAAAABR8/LQCliyz_jgI/photo.jpg"
} 

检查我可以看到一个电子邮件字段,但它是空的。

任何想法我缺少什么?

虽然我使用的范围不会提示用户额外的权限,但有没有我可以删除不需要的范围?

谢谢, 保罗

【问题讨论】:

  • 你能告诉我们谷歌给出的响应(HTTP状态)吗?
  • 切换到 HttpResponse r = oauth2.userinfo().v2().me().get().executeUnparsed();我看到 200,“OK”
  • 您的回复包含这样的“电子邮件”? "kind": "plus#person", "etag": "\"rthtrsgd\"", "gender": "male", "emails": [ { "value": "28@gmail.com", "type": "account" } ], "objectType": "person", "id": "XXXXXXXXXXXXXXXXX",`
  • 更新问题以显示不显示空值的 toString 输出。在调试器中检查也显示 email 为空。
  • 我想它想念你的范围googleapis.com/auth/userinfo.email

标签: java oauth-2.0 google-api


【解决方案1】:

通过将范围设置为“电子邮件”、进行身份验证并使用 com.google.api.client.googleapis.auth.oauth2.GoogleCredential 和 com.google.api.services.oauth2.Oauth2,我能够接收电子邮件:

        GoogleCredential credential = new GoogleCredential().setAccessToken(tokenFromAuthentication); 
        Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
                .setApplicationName("Oauth2")
                .build();
        Userinfoplus userinfo = oauth2.userinfo().get().execute();

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 2021-12-21
    • 1970-01-01
    • 2015-08-23
    相关资源
    最近更新 更多