Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Google default credentials on Heroku without the JSON file?

I'm looking to deploy a Node app to Heroku, and the key challenge I'm running into has to do with the Google default authorization workflow for Node. By default, Google looks for a JSON file with secret keys, with GOOGLE_APPLICATION_CREDENTIALS as the environmental variable name that points to the path of this JSON file. That is fine for local development, but in production I naturally do not want to commit this sensitive JSON file to source. Heroku allows you to create environmental variables, but each variable is individual. Somehow I need to break up this JSON file into individual variables, but I don't know what to call them for Google to recognize them.

There is a similar thread on this for Ruby, but the equivalent does not work in Node.

like image 946
user3621334 Avatar asked Apr 14 '17 04:04

user3621334


Video Answer


1 Answers

When using the google translation api, I ran into the same issue. I was unable to reference the whole JSON file, so I created two env variables in Heroku and referenced them in a credentials object. You can not have them stand alone. The .replace for the private key is an important detail. You should paste in that full key as is on Heroku.

const Translate = require('@google-cloud/translate');
const projectId = 'your project id here';

const translate = new Translate({
  projectId: projectId,
  credentials: {
    private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n'),
    client_email: process.env.GOOGLE_CLIENT_EMAIL
  }
});
like image 144
user3113645 Avatar answered Sep 20 '22 04:09

user3113645