Google-cloud-node: keyFilename from JSON string instead of file

Created on 18 Jul 2018  路  10Comments  路  Source: googleapis/google-cloud-node

Is it possible to pass a JSON string value for keyFilename instead of a local file path? Example usage is if keyFilename is stored in a database as a sting and don't want to write to disk.

question

Most helpful comment

The contents of the key file are what you have to work with? You can provide a credentials object with the contents of a JSON key file:

new Storage({
  credentials: require('service-account-key-file.json') // (or just a raw JS object)
})

All 10 comments

The contents of the key file are what you have to work with? You can provide a credentials object with the contents of a JSON key file:

new Storage({
  credentials: require('service-account-key-file.json') // (or just a raw JS object)
})

jeez why isn't this documented!

Holy wow, what! WHY ISNT THIS DOCUMENTED LOL. ty for saving me. took me 2hrs to find this post.

:( What path did you take for docs? We definitely want to make sure we communicate this stuff properly.

:( What path did you take for docs? We definitely want to make sure we communicate this stuff properly.

https://github.com/googleapis/nodejs-storage which took me to https://cloud.google.com/docs/authentication/getting-started

Mostly only points to setting the environment variable to the path of the JSON file. I wanted to pass the JSON data via the environment variable instead. So I looked around how to just pass the data instead of letting the SDK search for the environment variable itself.

I googled for a bit and found the client.authClient.fromJSON(). Though after instantiating Storage() and then setting it fromJSON(), it still didn't work. I don't think I found anywhere but here mentioning I can pass it via credentials. Also, my bad because even though I checked the source it slipped my mind to check the parent of StorageOptions :(

Was there another pathway I missed? Please let me know! Thanks for the response!

Just for reference, I was having some trouble with actually getting the credentials in, so here are some things to note:

  1. Make sure that the credentials line has the PARSED version of the JSON: credentials: JSON.parse(process.env.GOOGLE_KEY)
  2. In the .env file, make sure that the JSON is on 1 line but does NOT have a starting and ending quotation mark: GOOGLE_KEY={"start": "json", "key":"here"....} (example what NOT to do: GOOGLE_KEY="{"start":"json", "key":"here"...}"

Thanks @dannydenenberg https://github.com/googleapis/google-cloud-node/issues/2847#issuecomment-667687400, the credentials piece was the trick for me. Most examples were showing only the keyFile, but switching to credentials worked perfectly when using the googleapis package.

Example .env

YOUR_GOOGLE_JSON_KEY={"type":"service_account","project_id":"XXXXX",....}

Example using Google Analytics API:

import { google } from "googleapis";

const analytics = google.analytics({
  version: "v3",
  auth: new google.auth.GoogleAuth({
    // Your Google service account JSON key as a string. 
    credentials: JSON.parse(process.env.YOUR_GOOGLE_JSON_KEY), // make sure to replace with whatever you call your `.env` variable,
    scopes: ["https://www.googleapis.com/auth/analytics.readonly"]
  })
});

The contents of the key file are what you have to work with? You can provide a credentials object with the contents of a JSON key file:

new Storage({
  credentials: require('service-account-key-file.json') // (or just a raw JS object)
})
bro you are GOD 馃槶馃槶馃槶馃槶

The contents of the key file are what you have to work with? You can provide a credentials object with the contents of a JSON key file:

new Storage({
  credentials: require('service-account-key-file.json') // (or just a raw JS object)
})

Thanks, the docs say nothing about this. Crazy.

This was way harder to find than it needed to be

Was this page helpful?
0 / 5 - 0 ratings