node.js - 使用生成令牌的客户端从服务器端的人员 API 中检索生日和性别
问题描述
我想从我的后端 nodejs 服务器中的 google people API 中检索生日和性别。访问令牌是在这两个范围的客户端生成的: https://www.googleapis.com/auth/user.birthday.read https://www.googleapis.com/auth/userinfo.profile
客户端发送 accessToken,服务器通过以下方式查询人员 API:
const {google} = require('googleapis');
async function getDataFromPeopleAPI(googleId, accessToken) {
try {
let params = {
resourceName: `people/${googleId}`,
personFields: 'birthdays,genders',
access_token: accessToken //generated by client
};
let res = await google.people({
auth: GOOGLE_API_KEY //API key
}).people.get(params);
let {birthdays, genders} = res.data;
} catch (e) {
}
};
问题是,即使我的生日设置为公开且我的性别,people api 总是返回相同的结果。我没有收到任何错误代码,但我从未收到我想要的数据。这是我得到的回复:
"resourceName": "people/102865456870877320332",
"etag": "%EgUBBwg3LhoEAQIFBw=="
}
查询人员 API 时我做错了什么?谢谢 !
解决方案
这可能太老了,无法回答,但这是请求的正确格式:
const {google} = require('googleapis');
let userData = await google
.people({
version: "v1", // mention the API version
auth: process.env.GOOGLE_SERVER_API_KEY,
})
.people.get({
resourceName: "people/me", // not people/${googleId}
personFields: "genders,birthdays", // mention your scopes
access_token: accessToken, // generated by client
});
有关范围文档,请参阅此 URL: https ://developers.google.com/people/api/rest/v1/people/get
推荐阅读
- substrate - 如何在基材墨水上定义自己的结构
- android - Android 10 及更高版本中的 Wi-Fi 断开和重新连接
- android - 包 com.mikepenz.community_material_typeface_library 不存在
- tableau-desktop - 连接到 Tableau 中的存储过程时出错
- ios - iOS:使网站只能嵌入到我的应用程序中
- android - Android:如何从依赖图中删除 org.bouncycastle:bcpkix-jdk15on:1.56 并使用 org.bouncycastle:bcpkix-jdk15on:1.65
- javascript - 在 mac 上更新软件后,我无法运行 sudo npm i -g expo cli 命令
- python - 在 discord.py 中踢/禁止人
- spss-modeler - SPSS Modeler 实时预测服务
- c# - 在单独的类中删除带有按钮的文本框