![](/img/trans.png)
[英]How can I get a user's Google account photo using IAP and the People API? (NodeJS preferred)
[英]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);
}
});
这是我得到的回应。
我几乎尝试了所有方法,但仍然无法获得性别和生日。
为了从经过身份验证的用户那里获取有关gender
和birthdays
的信息,您可以使用resourceName=people/me
和personFields=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 快速入门。genders
和birthdays
都被添加到经过身份验证的用户的帐户中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.