【发布时间】:2017-12-26 01:04:04
【问题描述】:
我关注了instructions 使用 Google Translate API。我有:
- 创建了一个项目
- 已启用结算功能并在结算帐户上有 300 美元
- 启用翻译 API 并确保配额很高
- 生成了一个具有管理员角色的服务帐户并下载了 JSON
-
将以下行添加到我的 .zshrc:
export GOOGLE_APPLICATION_CREDENTIALS=/pathto/Holodeck-421412.json -
来源:
source ~/.zshrc
然而我的 nodeJS 代码和我的 curl 都返回了:
code: 403,
errors:
[ { message: 'Daily Limit Exceeded',
domain: 'usageLimits',
reason: 'dailyLimitExceeded' } ],
response: undefined,
message: 'Daily Limit Exceeded' }
我的卷发:
curl --header "Content-Type: application/json" --header "Authorization: Bearer `gcloud auth print-access-token`" --show-error -s "https://translation.googleapis.com/language/translate/v2" -d @translate-request.json
和翻译-request.json:
{
"q": "The quick brown fox jumped over the lazy dog.",
"source": "en",
"target": "es",
"format": "text"
}
我的 NodeJS 代码:
// Imports the Google Cloud client library
const language = require('@google-cloud/language');
const Translate = require('@google-cloud/translate');
// Instantiates a client
const client = new language.LanguageServiceClient();
// Your Google Cloud Platform project ID
const projectId = 'myproject ID';
// Instantiates a client
const translation = new Translate({
projectId: projectId
});
// The text to analyze
let text1 = 'Hello, world!';
const translate = (text) => {
const target = 'en';
translation
.translate(text, target)
.then(results => {
const translation = results[0];
console.log(`Text: ${text}`);
console.log(`Translation: ${translation}`);
})
.catch(err => {
console.error('ERROR:', err);
});
}
const analyze = (text) => {
const document = {
content: "good very good amazingly good",
type: 'PLAIN_TEXT',
};
// Detects the sentiment of the text
client
.analyzeSentiment({document: document})
.then(results => {
const sentiment = results[0].documentSentiment;
console.log(`Sentiment score: ${sentiment.score}`);
console.log(`Sentiment magnitude: ${sentiment.magnitude}`);
})
.catch(err => {
console.error('ERROR:', err);
});
}
translate(text1);
analyze(text1);
让我感到困惑的是,自然语言 API 正在运行,因此服务帐户似乎运行正常。这有什么角度吗?我花了 3 个小时试图通过这个意想不到的障碍,并且我已经完成了我能想到的任何牦牛剃须,包括打开新项目/服务帐户/API 密钥和谷歌群组论坛(它的设计只是让我更欣赏 StackOverflow... :))
----更新----
当我将 Characters per day 的配额从 1,000,000 更改为另一个值时,API 似乎开始工作 15 秒左右(2-3 个请求),然后返回 403 错误。然后,如果我再次更改配额,我会收到另一轮 2-3 个请求。就好像请求本身正在更改配额或更改在 15-20 秒后撤消。
【问题讨论】:
标签: google-cloud-platform google-translate