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

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
2个回答

3

您可能需要不同的作用域。根据文档,作用域https://www.googleapis.com/auth/userinfo.profile允许您查看基本的个人资料信息。若要查看电子邮件,则需要此作用域:https://www.googleapis.com/auth/userinfo.email


现在我可以获取电子邮件,以下是响应: "{ "id": "00000000000000", "email":"abc.xyz.com", "name": "Fred Example", "given_name": "Fred", "family_name": "Example", "picture": "https://lh5.googleusercontent.com/-2Sv-/photo.jpg", "locale": "en" }", 如何使用此响应检索电子邮件? - Mahendra
var email = JSON.parse(response)["email"]; - Iván Nokonoko
@Gowri https://www.googleapis.com/auth/userinfo.email 不是 API 端点,而是范围。您必须发送授权请求的 API 端点是 https://www.googleapis.com/auth/userinfo - Iván Nokonoko
@Gowri 我正在尝试连接到此地址,但仍未收到电子邮件 [link](https://www.googleapis.com/oauth2/v3/userinfo?alt=json&access_token=SOMETOKEN&scope=https://www.googleapis.com/auth/userinfo.email) - Nadin Martini
您收到此错误是因为您的输入OAuth2作用域名称无效,或者它引用了一个更新的作用域,超出了此遗留API的范围。该API是在作用域名称格式尚未标准化的时期构建的。现在不再是这种情况,所有有效的作用域名称(新旧)都在https://developers.google.com/identity/protocols/oauth2/scopes上进行了编目。使用该网页手动查找与您正在尝试调用的API相关联的作用域名称,并将其用于制作OAuth2请求。这是我的最后回复。 - Nadin Martini
显示剩余2条评论

0

现在我能够在响应中获取电子邮件,以下是代码,我在清单中更改了Response

"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":"abc@xyz.com",
  "given_name": "Fred",
  "family_name": "Example",
  "picture": "https://lh5.googleusercontent.com/-2Sv-4bBMLLA/AAAAAAAAAAI/AAAAAAAAABo/bEG4kI2mG0I/photo.jpg",
  "locale": "en"
}"

3
我没有收到“userinfo.email”的响应,请帮忙。 - Gowri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接