如何使用谷歌访问令牌获取电子邮件地址?

问题描述 投票:0回答:3

我无法从回复中获取电子邮件 ID 回复如下:

{
  "id": "00000000000000",
  "name": "Fred Example",
  "given_name": "Fred",
  "family_name": "Example",
  "picture": "https://lh5.googleusercontent.com/-2Sv-4bBMLLA/AAAAAAAAAAI/AAAAAAAAABo/bEG4kI2mG0I/photo.jpg",
  "locale": "en"
}

这就是我正在使用的:

https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + token

javascript google-chrome-extension oauth-2.0
3个回答
3
投票

您可能需要不同的范围。根据文档,范围

https://www.googleapis.com/auth/userinfo.profile
允许您查看基本的个人资料信息。为了查看电子邮件,您需要此范围:
https://www.googleapis.com/auth/userinfo.email


0
投票

现在我可以收到回复电子邮件了,下面是代码,我在其中更改了清单中的响应

"oauth2": {
"client_id": "854569859639-9c0reicivklkv0ltpakv99iajqts5obs.apps.googleusercontent.com",
 "scopes": [ "https://www.googleapis.com/auth/userinfo.email" ]}

我将范围

"scopes": [ "https://www.googleapis.com/auth/plus.login" ]
更改为
"scopes": [ "https://www.googleapis.com/auth/userinfo.email" ]

以下是回复

 "{
  "id": "00000000000000",
  "name": "Fred Example",
  "email":"[email protected]",
  "given_name": "Fred",
  "family_name": "Example",
  "picture": "https://lh5.googleusercontent.com/-2Sv-4bBMLLA/AAAAAAAAAAI/AAAAAAAAABo/bEG4kI2mG0I/photo.jpg",
  "locale": "en"
}"

0
投票

它说:

从源“https://www.prokademi.com”访问“https://www.googleapis.com/auth/userinfo.email”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。

© www.soinside.com 2019 - 2024. All rights reserved.