node.js - 如何从 Google API 获取用户信息配置文件?

标签 node.js oauth google-api google-oauth

我必须通过 google 帐户实现登录。

我想要一些建议。

我在谷歌控制台中创建了项目。添加范围用户信息.profile

我正在按照互联网上的类(class)说明进行操作,但我仍然无法获取用户信息(电子邮件、姓名、年龄...)。

步骤:

{
    "azp": "155122683461-51hq2n932svo4ajbt98ic0q67m4tuj5o.apps.googleusercontent.com",
    "aud": "155122683461-51hq2n932svo4ajbt98ic0q67m4tuj5o.apps.googleusercontent.com",
    "sub": "108865940357700877124",
    "scope": "https://www.googleapis.com/auth/userinfo.profile",
    "exp": "1554094721",
    "expires_in": "3326",
    "access_type": "offline"
}

你们能给我举个例子吗:(

谢谢

最佳答案

人员接口(interface)

您要查找的信息可以在people.get 上找到

GET https://people.googleapis.com/v1/{resourceName=people/*}

提示发送没有空格的字段掩码 - person.emailAddresses,person.birthdays 它显示为 person info因此用户必须填写此信息

但是你需要添加范围来获取你想要的信息

https://www.googleapis.com/auth/profile.emails.read
https://www.googleapis.com/auth/user.birthday.read

你可以在这里测试Google Apis explorer

可以找到 google people api 的 node.js 快速入门 here

用户信息端点

也可以使用 userinfo 端点,但它不会返回您要查找的信息

您需要请求电子邮件范围才能在下面的响应中看到电子邮件,用户必须授予您查看他们电子邮件的权限以下是仅针对配置文件范围的标准响应。

GET /oauth2/v2/userinfo HTTP/1.1
Host: www.googleapis.com
Content-length: 0
Authorization: Bearer ya29.GlveBiwp4-NTPLU9VN3rn1enty11KOdQHGcyfZd1xJ1Ee9eGS2Pw2nJ7KDUBQPa-uT-AoKDQdoVigU6bruVIB1a3fiBu1n

回应

{
  "picture": "https://lh5.googleusercontent.com/-a1CWlFnA5xE/AAAAAAAAAAI/AAAAAAAAl1I/UcwPajZOuN4/photo.jpg", 
  "name": "Linda Lawton", 
  "family_name": "Lawton", 
  "locale": "en", 
  "gender": "female", 
  "link": "https://plus.google.com/+LindaLawton", 
  "given_name": "Linda", 
  "id": "117200475532672775346"
}

范围

你应该咨询 Node tutorial了解如何使用范围。请记住,如果您更改代码中的范围,您将需要再次请求用户的访问权限。

const SCOPES = ['profile', 'email'];

关于node.js - 如何从 Google API 获取用户信息配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55448883/

相关文章:

google-api - 查询 Picasa 网络相册中的特定 PhotoID 以检查是否存在

google-api - 如何从 OAuth 同意屏幕中删除 plus.me

node.js - 在 node.js 中执行并获取 shell 命令的输出

node.js - child_process.exec 产生 "♀"字符

node.js - NodeJs/Express 在浏览器中显示pdf文件

c# - Twitter 实现 OAuth 是否超出规范? (C#)

node.js - 将 Angular 应用程序部署到 AWS Elastic beanstalk

java - 保护 REST API

angularjs - 我缺少什么? Node.js strip 连接集成

google-api - 将事件添加到 Google 日历不起作用