首页 > 解决方案 > 尝试使用谷歌示例创建公司时出错

问题描述

我正在尝试在 v3 上使用 Google Talent Solution 创建一家公司,但我遇到了错误。(请注意,如果我回滚到 v2 我是成功的,但是 v2 似乎已被弃用)

Error: Unable to load endpoint jobs("v3"): ctr is not a constructor
    at Object.getAPI (/Users/me/Sites/match/node_modules/googleapis/build/src/shared/src/apiIndex.js:37:15)
    at GoogleApis.jobs (/Users/me/Sites/match/node_modules/googleapis/build/src/apis/jobs/index.js:22:18)
    at google.auth.getApplicationDefault (/Users/me/Sites/match/utils/company.js:21:35)
    at /Users/me/Sites/match/node_modules/google-auth-library/build/src/auth/googleauth.js:179:45
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

这似乎是 apiIndex.js 没有 v3 作为支持的版本。我正在使用来自 npm 的最新版本的 googleapis 32.0.0。

这是我尝试示例的 URL:

https://cloud.google.com/talent-solution/job-search/docs/before-you-begin

// Imports the Google APIs client library
const {google} = require('googleapis');
const projectId = process.env.GOOGLE_CLOUD_PROJECT;

// Acquires credentials
google.auth.getApplicationDefault((err, authClient) => {
  if (err) {
    console.error('Failed to acquire credentials');
    console.error(err);
    return;
  }

  if (authClient.createScopedRequired && authClient.createScopedRequired()) {
    authClient = authClient.createScoped([
      'https://www.googleapis.com/auth/jobs'
    ]);
  }

  // Instantiates an authorized client
  const jobService = google.jobs({
    version: 'v3',
    auth: authClient
  });

  const request = {
    parent: `projects/${projectId}`,
  };

  // Lists companies
  jobService.projects.companies.list(request, function (err, result) {
    if (err) {
      console.error('Failed to retrieve companies! ' + err);
      throw err;
    }
    console.log(`Request ID: ${result.data.metadata.requestId}`);

    const companies = result.data.companies || [];

    if (companies.length) {
      console.log('Companies:');
      companies.forEach((company) => console.log(company.name));
    } else {
      console.log(`No companies found.`);
    }
  });
});

标签: node.jsgoogle-api

解决方案


在联系节点模块的维护人员后,我能够解决这个问题。

如果其他人发现此问题并需要帮助,这是我的请求示例。

   const { google } = require('googleapis');

module.exports = () => {
  /**
  * createServiceConnection() Create a service connection to google for future queries
  * @return {object} service connection object
  */
  async function createServiceConnection() {
    return new Promise((resolve, reject) => {
      google.auth.getApplicationDefault((err, authClient) => {
        if (err) {
          console.error('Failed to acquire credentials', err); // eslint-disable-line no-console
          return reject({ code: 500, message: 'Failed to acquire credentials' });
        }

        if (authClient.createScopedRequired && authClient.createScopedRequired()) {
          authClient = authClient.createScoped([
            'https://www.googleapis.com/auth/jobs'
          ]);
        }

        // Instantiates an authorized client
        const jobService = google.jobs({
          version: 'v3',
          auth: authClient
        });

        return resolve(jobService);
      });
    });
  }


  return {
    createServiceConnection
  };
};

用这个调用

  app.service = await googleUtil.createServiceConnection();

然后我在其他地方使用它

const data = {
  parent: `projects/${process.env.GOOGLE_CLOUD_PROJECT}`,
  requestBody: {
    company: _data
  }
};

result = await app.service.projects.companies.create(data);

推荐阅读