user2028856
user2028856

Reputation: 3183

Google translate key missing on node js ajax

I have a simple Node JS script and which works fine when run locally in terminal:

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);
});
}

However whenever I call this via Ajax, I get the following error:

Error: The request is missing a valid API key.

I already added this as a permanent environmental variable using this:

export GOOGLE_APPLICATION_CREDENTIALS="[PATH to key downloaded]"

But still each time I call this script via Ajax, I get the same error. So my question is, how can I get the Node JS script to save the API key so that it works when called via Ajax?

Thanks

Upvotes: 0

Views: 409

Answers (2)

ChilTest
ChilTest

Reputation: 511

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();

You must take this credentials.json file from your project on google cloud. They will provide you a file .json

Upvotes: 0

Krzysztof Krzeszewski
Krzysztof Krzeszewski

Reputation: 6714

It seems that for whatever reason, the application cannot read the environmental variable correctly. Since nodejs stores all environmental variables in the process.env you could ensure that it is written by calling:

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);
}

or pass the key directly to the constructor with

const translate = new Translate.Translate({
    projectId: 'my-project',
    keyFilename: "[PATH to key downloaded]"
});

You can also ensure the key file is read on your end and just pass the config to the translate constructor

const translate = new Translate.Translate({
    credentials: JSON.parse(fs.readFileSync("[PATH to key downloaded]", "utf8"))
});

if it still does not help, maybe it's the issue with a key itself, and you could try generating a new one here https://console.cloud.google.com/apis/credentials

Upvotes: 3

Related Questions