无法通过 google 人 API 获取用户 google 帐户性别和生日字段。 (使用 OAuth2 客户端)

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

我正在我的移动 flutter 应用程序中使用 google 实现身份验证。我在我的应用程序中得到 access_token,然后将它发送到用 Node.js 编写的后端。然后我需要获取用户基本信息+生日和性别。在 Google Cloud Platform 控制台中,我进行了所有配置,添加了某些范围,'https://www.googleapis.com/auth/user.birthday.read'、'https://www.googleapis.com/auth/user.gender.read'、。我启用了 Google 人 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/me 调用人员 API 的 people.getpersonFields=genders,birthdays:

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 quickstart.
  • 假定 gendersbirthdays 都添加到经过身份验证的用户的帐户中。