首页 > 解决方案 > 使用生成令牌的客户端从服务器端的人员 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 时我做错了什么?谢谢 !

标签: node.jsgoogle-people-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


推荐阅读