node.js - 尝试使用谷歌示例创建公司时出错
问题描述
我正在尝试在 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.`);
}
});
});
解决方案
在联系节点模块的维护人员后,我能够解决这个问题。
如果其他人发现此问题并需要帮助,这是我的请求示例。
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);
推荐阅读
- json - JSON如何搜索键名称值并打印包含不区分大小写的字符串或字符串集的对象?
- python - 计算球状星团的 rms 半径
- python - 如何确定您没有文档的 REST API 所需的身份验证类型?
- java - Spring Boot批处理MultiResourceItemReader:如何使用@Value()从jar位置读取文件
- google-api - 检索每个地点的多张图片
- python - 将整数列表转换为位列表 Python
- python-3.x - 如何将存储在 Google Drive 中的本地模块导入 Google Collab?
- python - 是否可以从部分 URL 将文件上传到 S3?文件.aa、文件.ab、文件.ac
- node.js - 谷歌云 CPU 分析未显示有用数据
- asp.net-core - 如何在不需要电子邮件确认的情况下更改 ASP.NET Identity 用户的电子邮件?