无法获得 people.connections.list 的回复

unable to get a response with people.connections.list

⚠️ 我在主线程中忘记了一个process.exit(0),所以应用程序在回调执行之前就被终止了。 这个代码示例很有魅力


这是来自 googleapis nodejs client 的代码 我有以下问题:

首先,我想使用 nodejs 应用程序获取一个用户的联系人列表

设置 OAuth2Client

所以我用这个代码设置了一个OAuth2Client

const {
  client_id: CLIENT_ID,
  client_secret: CLIENT_SECRET
} = require("./keys.json");
const REDIRECT_URL = "http://localhost:3000/oauth2callback";
const oAuth2Client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);

然后,使用临时服务器,我使用用户的凭据请求令牌:

function getGoogleCode() {
  return new Promise((resolve, reject) => {
    // Open an http server to accept the oauth callback. In this simple example, the
    // only request to our webserver is to /oauth2callback?code=<code>
    const server = http
      .createServer(async (req, res) => {
        if (req.url.indexOf("/oauth2callback") > -1) {
          // acquire the code from the querystring, and close the web server.
          const { code } = querystring.parse(url.parse(req.url).query);
          res.end(
            `Authentication successful! Please return to the console. [code: ${code}]`
          );
          server.close();
          resolve(code);
        }

        reject(new Error("oops", req, res));
      })
      .listen(3000, () => {
        // open the browser to the authorize url to start the workflow
        // Generate the url that will be used for the consent dialog.
        const authorizeUrl = oAuth2Client.generateAuthUrl({
          access_type: "offline",
          scope: ["https://www.googleapis.com/auth/contacts.readonly"]
        });
        opn(authorizeUrl);
      });
  });
}

然后我完成设置我的客户端:

const code = await getGoogleCode();
const { tokens } = await oAuth2Client.getToken(code);
oAuth2Client.setCredentials(tokens);

当一切都好时

我设法得到了低水平的回应API:

const personFields = ["emailAddresses", "names"];
const url = `https://people.googleapis.com/v1/people/me/connections?personFields=${personFields.join(
  ","
)}`;
const res = await oAuth2Client.request({ url });
console.log(chalk.gray(JSON.stringify(res.data.connections, null, 2)));

一切正常,但是,我想使用同一个库中的高级API

google.people API

API Explorer中所述,我构建了以下代码:

const personFields = ["emailAddresses", "names"];
people.people.connections.list(
  { resourceName: "people/me", personFields },
  (res, err) => {
    console.log("error: ", err);
    console.log("res1: ", res);
  }
);

没有错误,没有 res1,没有。


⚠️ 我在主线程中忘记了一个process.exit(0),所以应用程序在回调执行之前就被终止了。 这个代码示例很有魅力

不确定为什么没有记录。但是您可能应该 return 回调中的 res,否则在回调中调用 await 将 return undefined.

还要确保在 people.people.connections.list 调用中设置 personFields