【发布时间】:2017-07-18 22:15:27
【问题描述】:
我正在配置 Node.JS 服务器以使用 Google 翻译 API。到目前为止,我已经完成了以下工作:
- 设置 Google 帐户
- 添加信用卡并启用计费
- 创建项目
- 为项目启用 Google 翻译 API
- 为项目*创建两个服务帐号并保存 JSON 密钥文件
*一个服务帐户用于本地开发,一个帐户用于部署应用程序。
示例代码(打字稿):
import * as Translate from '@google-cloud/translate';
export async function translate(text: string, to: string): Promise<string> {
let translation = '';
const googleTranslate = Translate({
projectId: PROJECT_ID,
keyFilename: PATH/TO/KEY_FILE
});
const response = await googleTranslate.translate(text, to);
if (Array.isArray(response[0])) {
for (let t of response[0]) {
translation += t;
}
}
else {
translation = response[0];
}
return translation;
}
我在我的工作站上测试了本地和开发密钥并成功翻译。 但是,在部署的环境下(不同机器,动态IP)不起作用。出现如下错误:
错误响应:
{
domain: 'usageLimits',
reason: 'dailyLimitExceeded',
message: 'This API requires billing to be enabled on the project. Visit https://console.developers.google.com/billing?project=GOOGLE_PROJECT_ID to enable billing.',
extendedHelp: 'https://console.developers.google.com/billing?project=project=GOOGLE_PROJECT_ID'
}
计费已启用,所以我错过了什么?
【问题讨论】:
标签: amazon-ec2 chef-infra google-translate google-api-nodejs-client