繁体   English   中英

无法使用 google People API 获取用户 google 帐户性别和生日字段。 (使用 OAuth2client)

[英]Can't get user google account gender and birthday fields with google People API. (Using OAuth2client)

我正在我的移动 flutter 应用程序中使用谷歌实现身份验证。 我在我的应用程序中获得了 access_token,然后将其发送到使用 Node.js 编写的后端。 然后我需要获取用户基本信息+生日和性别。 在 Google Cloud Platform 控制台中,我进行了所有配置,添加了某些范围, “https://www.googleapis.com/auth/user.birthday.read”、“https://www.googleapis.com/auth/user”。性别.阅读', 我启用了 Google People API。 但我仍然无法得到生日和性别。 这是后端部分。

const token =
  "HARDCODED_ACCESS_TOKEN";

var google = require("googleapis").google;
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2();

oauth2Client.setCredentials({
  access_token: token,
  scope: "https://www.googleapis.com/auth/user.gender.read",
});

var oauth2 = google.oauth2({
  auth: oauth2Client,
  version: "v2",
});

oauth2.userinfo.get(function (err, res) {
  if (err) {
    console.log(err);
  } else {
    console.log(res.data);
  }
});

这是我得到的回应。

在此处输入图像描述

我几乎尝试了所有方法,但仍然无法获得性别和生日。

为了从经过身份验证的用户那里获取有关genderbirthdays的信息,您可以使用resourceName=people/mepersonFields=genders,birthdays调用 People API 的people.get

oauth2Client.setCredentials({
  access_token: token,
});
const service = google.people({version: 'v1', auth: oauth2Client});
service.people.get({
  resourceName: 'people/me',
  personFields: 'genders,birthdays'
}, (err, res) => {
  // Do your thing
});

笔记:

  • 您没有为大多数身份验证过程提供代码,但请注意,必须在检索access_token之前提供范围,因为访问令牌取决于这些范围。 另外,我建议您设置一个refresh_token ,因为access_token将在一小时内到期。 有关 OAuth 流程的更多信息,请查看Node.js 快速入门
  • 假设gendersbirthdays都被添加到经过身份验证的用户的帐户中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM