How set API KEY in Google Translate Node.js code
Asked Answered
B

4

11

I'm trying to create a Node.js code that uses Google Translate API. I got the code below from the Google doc

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()
Belch answered 17/4, 2019 at 16:57 Comment(1)
Why i got : const transl = new Translate(); ^ TypeError: Translate is not a constructorSpermatophyte
Y
4

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'
Yellowknife answered 17/4, 2019 at 17:10 Comment(4)
Worked very well!! Just one thing... on mac the command is ". ./.env"Belch
Thanks for the feedback, will edit. We can just say the same for both Mac and Linux.Yellowknife
where did you find the documentation that shows that export GOOGLE... is the right way? It works, but how did you find it?Belch
@YuriAps the way Google does this has definitely changed over the years. You can now use gcloud auth login which will set these environment variables for you.Yellowknife
B
5
  1. create api-key see documentation create api key doc
  1. use it like:
import { v2 } from '@google-cloud/translate';
const translateClint = new v2.Translate({
    projectId:'your-projectId-here',
    key: 'your-api-key-here',
});

I don't check it for v3, but I see the same interface: new v3.TranslationServiceClient({ key:"may be works", projectId:"may be works" })

Bautram answered 2/1, 2023 at 18:32 Comment(1)
Please note that this only works if you are using the v2 API client.Weevily
Y
4

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'
Yellowknife answered 17/4, 2019 at 17:10 Comment(4)
Worked very well!! Just one thing... on mac the command is ". ./.env"Belch
Thanks for the feedback, will edit. We can just say the same for both Mac and Linux.Yellowknife
where did you find the documentation that shows that export GOOGLE... is the right way? It works, but how did you find it?Belch
@YuriAps the way Google does this has definitely changed over the years. You can now use gcloud auth login which will set these environment variables for you.Yellowknife
S
2

Checkout this Google Authentication Page to add the key

  1. In the GCP Console, go to the Create service account key page.

  2. From the Service account list, select New service account.

  3. In the Service account name field, enter a name.

  4. From the Role list, select Project > Owner. Click

  5. Create. A JSON file that contains your key downloads to your computer.

and

export GOOGLE_APPLICATION_CREDENTIALS="[PATH to key downloaded]"
Sarajane answered 17/4, 2019 at 17:17 Comment(0)
S
0

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
                });
Succinctorium answered 7/7, 2021 at 21:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.