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

时间:2018-03-27 12:56:02

标签: javascript google-chrome-extension oauth-2.0

我无法从响应中获取电子邮件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 个答案:

答案 0 :(得分:2)

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

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