Google OAuth userinfo方法不返回电子邮件地址

问题描述

我将Google Api与Postman配合使用,并且正在使用我的令牌进行检索。

https://accounts.google.com/o/oauth2/token?code={my_code}

enter image description here

现在,使用 access_token ,我希望获得用户数据 所以我叫这个方法

https://www.googleapis.com/oauth2/v3/userinfo?access_token={my_access_token}

但是我的答复是

enter image description here

我已经尝试了userinfo.email,但是没有找到共鸣

enter image description here

有没有办法获取用户电子邮件? 谢谢


更新:也尝试将范围设置为auth调用

enter image description here

和userinfo调用

enter image description here

但是没有返回电子邮件

解决方法

来自documentation

电子邮件仅在您的范围包括电子邮件范围值时提供。

为此,请为您的应用程序设置一个附加范围:

https://www.googleapis.com/auth/userinfo.email

您可以转到开发者控制台,然后在customizing用户同意屏幕上添加范围。这里的目标是您每次查看userinfo.emailuserinfo.profile范围 使用邮递员检索您的访问令牌。

另一种方法是您必须再次生成另一个authorization code。授权时,系统会要求您在请求中包含一个范围,并在其中包含所需的两个范围。

,

未设计或保证userinfo.endpoint每次都返回要求保护的内容。如果您想每次获取用户的电子邮件地址并包含电子邮件范围,则应该使用People api。

People.get

curl \
  'https://people.googleapis.com/v1/people/me?personFields=emailAddresses' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

响应

{
  "resourceName": "people/117200475532672775346","etag": "%EgUBCT43LhoEAQIFByIMR3BzQkR2cnI1ZGc9","emailAddresses": [
    {
      "metadata": {
        "primary": true,"verified": true,"source": {
          "type": "ACCOUNT","id": "117200475532672775346"
        }
      },"value": "xxxx@gmail.com"
    },{
      "metadata": {
        "verified": true,"source": {
          "type": "PROFILE","value": "xxxx@gmail.com","type": "home","formattedType": "Home"
    },{
      "metadata": {
        "source": {
          "type": "CONTACT","id": "3faa96eb08baa4be"
        }
      },"value": "xxxx@hotmail.com","type": "other","formattedType": "Other"
    },"value": "xxxx@daimto.com","value": "xxxx@eg.dk","type": "work","formattedType": "Work"
    }
  ]
}

除了我认为您应该考虑使用邮递员中的auth选项卡并选择oauth2之外,您还应该能够在其中添加客户端ID和所有内容。然后自己换掉所有代码会容易得多。

,

尝试使用此端点:

https://openidconnect.googleapis.com/v1/userinfo

您可以通过以下网址检查所有OAuth终结点:https://accounts.google.com/.well-known/openid-configuration