如何在Google中设置API KEY 翻译Node.js代码
How set API KEY in Google Translate Node.js code
我正在尝试创建一个使用 google 翻译 api 的 Node.js 代码。
我从 google 文档 (https://cloud.google.com/translate/docs/translating-text)
中获得了以下代码
但是当我 运行 它时,它说 "Error: The request is missing a valid API key."
我有钥匙,但我不知道如何以及在哪里设置它。
async function translate() { // Imports the Google Cloud client library
const { Translate } = require('@google-cloud/translate');
// Creates a client
const translate = new Translate();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
const text = 'Hello, world!';
const target = 'ru';
// 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.forEach((translation, i) => {
console.log(`${text[i]} => (${target}) ${translation}`);
});
}
translate()
This page on setting up authentication 说明您需要从创建服务帐户密钥页面下载凭据文件。然后可以将其添加到您的路径 (.bashrc
),如下所示:
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
或者,您可以将上面的行添加到项目根目录下的 .env
文件中,并在您 运行 应用程序时获取它:
. ./.env
npm start
或
sh -ac '. ./.env; npm start'
检查此 Google Authentication Page 以添加密钥
In the GCP Console, go to the Create service account key page.
From the Service account list, select New service account.
In the Service account name field, enter a name.
From the Role list, select Project > Owner. Click
Create. A JSON file that contains your key downloads to your computer.
和
export GOOGLE_APPLICATION_CREDENTIALS="[PATH to key downloaded]"
试试这个...没有环境变量,请...将此文件添加到您的 .gitignore
import * as credentials from 'credentials.json';
...
const {Translate} = require('@google-cloud/translate').v2;
const translationApi = new Translate({
projectId:'your-project-id',
credentials:credentials
});
我正在尝试创建一个使用 google 翻译 api 的 Node.js 代码。 我从 google 文档 (https://cloud.google.com/translate/docs/translating-text)
中获得了以下代码但是当我 运行 它时,它说 "Error: The request is missing a valid API key." 我有钥匙,但我不知道如何以及在哪里设置它。
async function translate() { // Imports the Google Cloud client library
const { Translate } = require('@google-cloud/translate');
// Creates a client
const translate = new Translate();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
const text = 'Hello, world!';
const target = 'ru';
// 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.forEach((translation, i) => {
console.log(`${text[i]} => (${target}) ${translation}`);
});
}
translate()
This page on setting up authentication 说明您需要从创建服务帐户密钥页面下载凭据文件。然后可以将其添加到您的路径 (.bashrc
),如下所示:
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
或者,您可以将上面的行添加到项目根目录下的 .env
文件中,并在您 运行 应用程序时获取它:
. ./.env
npm start
或
sh -ac '. ./.env; npm start'
检查此 Google Authentication Page 以添加密钥
In the GCP Console, go to the Create service account key page.
From the Service account list, select New service account.
In the Service account name field, enter a name.
From the Role list, select Project > Owner. Click
Create. A JSON file that contains your key downloads to your computer.
和
export GOOGLE_APPLICATION_CREDENTIALS="[PATH to key downloaded]"
试试这个...没有环境变量,请...将此文件添加到您的 .gitignore
import * as credentials from 'credentials.json';
...
const {Translate} = require('@google-cloud/translate').v2;
const translationApi = new Translate({
projectId:'your-project-id',
credentials:credentials
});