Google 节点 js 上缺少翻译键 ajax
Google translate key missing on node js ajax
我有一个简单的 Node JS 脚本,当 运行 在终端本地运行时它工作正常:
exports.google_translate = function (translate_text, res) {
var Translate = require('@google-cloud/translate');
var translate = new Translate.Translate({projectId: 'my project'});
translate.translate(translate_text, 'fr').then(results => {
var translation = results[0];
res.send(translation);
}).catch(err => {
res.send('ERROR:', err);
});
}
然而,每当我通过 Ajax 调用它时,我都会收到以下错误:
Error: The request is missing a valid API key.
我已经使用这个将其添加为永久环境变量:
export GOOGLE_APPLICATION_CREDENTIALS="[PATH to key downloaded]"
但每次我通过 Ajax 调用此脚本时,我仍然会遇到同样的错误。所以我的问题是,如何让 Node JS 脚本保存 API 键,以便它在通过 Ajax 调用时工作?
谢谢
看来是因为什么原因,应用程序无法正确读取环境变量。由于 nodejs 将所有环境变量存储在 process.env
中,您可以确保它是通过调用来编写的:
function google_translate(translate_text) {
process.env.GOOGLE_APPLICATION_CREDENTIALS = "[PATH to key downloaded]";
return translate.translate(translate_text, 'fr')
.then(console.log)
.catch(console.error);
}
或者通过
将密钥直接传递给构造函数
const translate = new Translate.Translate({
projectId: 'my-project',
keyFilename: "[PATH to key downloaded]"
});
您还可以确保在您这边读取密钥文件,然后将配置传递给翻译构造函数
const translate = new Translate.Translate({
credentials: JSON.parse(fs.readFileSync("[PATH to key downloaded]", "utf8"))
});
如果还是不行,可能是密钥本身的问题,你可以尝试在这里生成一个新密钥https://console.cloud.google.com/apis/credentials
const {Translate} = require('@google-cloud/translate').v2;
const translate = new Translate({
credentials: {
"type": "account",
"project_id": "your_project",
"private_key_id": "your_data",
"private_key": "your_data",
"client_email": "your_data",
"client_id": "your_data",
"auth_uri": "your_data",
"token_uri": "your_data",
"auth_provider_x509_cert_url": "your_data",
"client_x509_cert_url": "your_data"
}
});
const text = 'This is testing!';
const target = 'de';
async function translateText() {
// Translates the text into the target language. "text" can be a string for
// translating a single piece of text, or an array of strings for translating
// multiple texts.
let [translations] = await translate.translate(text, target);
translations = Array.isArray(translations) ? translations : [translations];
console.log('Translations:' + translations);
}
translateText();
您必须从 google 云上的项目中获取此 credentials.json
文件。他们会给你一个文件 .json
我有一个简单的 Node JS 脚本,当 运行 在终端本地运行时它工作正常:
exports.google_translate = function (translate_text, res) {
var Translate = require('@google-cloud/translate');
var translate = new Translate.Translate({projectId: 'my project'});
translate.translate(translate_text, 'fr').then(results => {
var translation = results[0];
res.send(translation);
}).catch(err => {
res.send('ERROR:', err);
});
}
然而,每当我通过 Ajax 调用它时,我都会收到以下错误:
Error: The request is missing a valid API key.
我已经使用这个将其添加为永久环境变量:
export GOOGLE_APPLICATION_CREDENTIALS="[PATH to key downloaded]"
但每次我通过 Ajax 调用此脚本时,我仍然会遇到同样的错误。所以我的问题是,如何让 Node JS 脚本保存 API 键,以便它在通过 Ajax 调用时工作?
谢谢
看来是因为什么原因,应用程序无法正确读取环境变量。由于 nodejs 将所有环境变量存储在 process.env
中,您可以确保它是通过调用来编写的:
function google_translate(translate_text) {
process.env.GOOGLE_APPLICATION_CREDENTIALS = "[PATH to key downloaded]";
return translate.translate(translate_text, 'fr')
.then(console.log)
.catch(console.error);
}
或者通过
将密钥直接传递给构造函数const translate = new Translate.Translate({
projectId: 'my-project',
keyFilename: "[PATH to key downloaded]"
});
您还可以确保在您这边读取密钥文件,然后将配置传递给翻译构造函数
const translate = new Translate.Translate({
credentials: JSON.parse(fs.readFileSync("[PATH to key downloaded]", "utf8"))
});
如果还是不行,可能是密钥本身的问题,你可以尝试在这里生成一个新密钥https://console.cloud.google.com/apis/credentials
const {Translate} = require('@google-cloud/translate').v2;
const translate = new Translate({
credentials: {
"type": "account",
"project_id": "your_project",
"private_key_id": "your_data",
"private_key": "your_data",
"client_email": "your_data",
"client_id": "your_data",
"auth_uri": "your_data",
"token_uri": "your_data",
"auth_provider_x509_cert_url": "your_data",
"client_x509_cert_url": "your_data"
}
});
const text = 'This is testing!';
const target = 'de';
async function translateText() {
// Translates the text into the target language. "text" can be a string for
// translating a single piece of text, or an array of strings for translating
// multiple texts.
let [translations] = await translate.translate(text, target);
translations = Array.isArray(translations) ? translations : [translations];
console.log('Translations:' + translations);
}
translateText();
您必须从 google 云上的项目中获取此 credentials.json
文件。他们会给你一个文件 .json