尝试使用Google示例创建公司时出现错误

时间:2018-08-15 17:49:21

标签: node.js google-api

我正在尝试使用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。

这里是我尝试示例的网址:

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.`);
    }
  });
});

1 个答案:

答案 0 :(得分:0)

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

这是我的请求的一个示例,如果其他人找到它并需要帮助。

   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);