问题描述
我正在尝试创建使用Google Translate API的Node.js代码. 我从Google Doc( https://cloud.google.com/translate.com/translate.com/docs/translating-text )
但是,当我运行它时,它说"错误:请求缺少有效的API密钥". 我有钥匙,但我不知道如何和何处设置它.
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()
推荐答案
设置身份验证的此页面解释您需要说明您需要从创建服务帐户密钥页面下载凭据文件.然后可以将其添加到您的路径(.bashrc)中,如下所示:
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
另外,您可以将上面的行添加到项目root上的.env文件中,并在运行应用程序时源源:
. ./.env npm start
或
sh -ac '. ./.env; npm start'
其他推荐答案
结帐此 google Autherationical page 添加键/p>
在GCP控制台中,转到创建服务帐户密钥页面.
从服务帐户列表中,选择新服务帐户.
在"服务帐户名"字段中,输入名称.
从角色列表中,选择项目>所有者.单击
创建.一个包含您下载到计算机的密钥的JSON文件.
和
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 });
问题描述
I'm trying to create a Node.js code that uses google translate api. I got the code below from the google doc (https://cloud.google.com/translate/docs/translating-text)
But when I run it, it says "Error: The request is missing a valid API key." I have the key, but i don't know how and where to set it.
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 explains that you need to download a credentials file from the create service account key page. This can then be added to your path (.bashrc) as follows:
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
Alternately, you could add the line above to a .env file on your project root and source it when you are running the application:
. ./.env npm start
or
sh -ac '. ./.env; npm start'
其他推荐答案
Checkout this Google Authentication Page to add the key
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.
and
export GOOGLE_APPLICATION_CREDENTIALS="[PATH to key downloaded]"
其他推荐答案
Try this... no environment variables AND please... add this file to your .gitignore
import * as credentials from 'credentials.json'; ... const {Translate} = require('@google-cloud/translate').v2; const translationApi = new Translate({ projectId:'your-project-id', credentials:credentials });