google-cloud-node icon indicating copy to clipboard operation
google-cloud-node copied to clipboard

keyFilename from JSON string instead of file

Open nodesocket opened this issue 8 years ago β€’ 17 comments

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 string and don't want to write to disk.

nodesocket avatar Jul 18 '18 02:07 nodesocket

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

stephenplusplus avatar Jul 20 '18 15:07 stephenplusplus

jeez why isn't this documented!

askdesigners avatar Apr 28 '19 19:04 askdesigners

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

dakdevs avatar Jul 06 '20 20:07 dakdevs

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

stephenplusplus avatar Jul 06 '20 20:07 stephenplusplus

:( 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!

dakdevs avatar Jul 06 '20 20:07 dakdevs

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"...}"~~

dannydenenberg avatar Aug 02 '20 15:08 dannydenenberg

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

nwittwer avatar Aug 05 '20 10:08 nwittwer

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 😭😭😭😭

noob-sumit avatar Sep 14 '20 13:09 noob-sumit

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.

joaokrabbe avatar Sep 18 '20 21:09 joaokrabbe

This was way harder to find than it needed to be

ashkan-pm avatar Jul 13 '21 20:07 ashkan-pm

I used this with the latest version of @google-cloud/storage "@google-cloud/storage": "^6.6.0",

new Storage({
credentials: {
      type: 'service_account',
      project_id: 'xxxxxxx',
      private_key_id: 'xxxx',
      private_key:'-----BEGIN PRIVATE KEY-----xxxxxxx\n-----END PRIVATE KEY-----\n',
      client_email: 'xxxx',
      client_id: 'xxx',
      auth_uri: 'https://accounts.google.com/o/oauth2/auth',
      token_uri: 'https://oauth2.googleapis.com/token',
      auth_provider_x509_cert_url: 'https://www.googleapis.com/oauth2/v1/certs',
      client_x509_cert_url: 'xxx',
    },
})

yhau1989 avatar Nov 01 '22 05:11 yhau1989

I submitted pull request https://github.com/googleapis/nodejs-storage/pull/2123 with hope of landing that and helping other future developers to find this without having to google or scan the issue queue for clues.

lirantal avatar Dec 20 '22 08:12 lirantal

Thanks!!!! πŸ™

bombillazo avatar May 11 '23 12:05 bombillazo

It's the second half of 2024 and it's still too hard to find this information.

dwang-qm avatar Aug 25 '24 17:08 dwang-qm

I've been scouring the official docs for DAYS trying to search for this information. I really hope the documentation is improved – there's a lot of information but a lot of it is either straight up outdated or not remotely useful.

m-shum avatar Jan 13 '25 20:01 m-shum

Curious, what path did you take to the docs and where'd you end up? Maybe that's worth looking into as well as any changes to the docs themselves.

(Note: I'm not part of this project anymore, just interested as I was part of this discussion πŸ˜„ )

stephenplusplus avatar Jan 14 '25 20:01 stephenplusplus

Curious, what path did you take to the docs and where'd you end up? Maybe that's worth looking into as well as any changes to the docs themselves.

(Note: I'm not part of this project anymore, just interested as I was part of this discussion πŸ˜„ )

Stephen – primarily searched around this documentation https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries but also looked at https://googleapis.dev/nodejs/analytics-data/latest/index.html#installing-the-client-library and https://github.com/googleapis/google-cloud-node/tree/main/packages/google-analytics-data/samples because there's no real linear documentation for the client modules. The auth decision flow docs https://cloud.google.com/docs/authentication#service-accounts were by far the most confusing though. I don't remember how I ended up here but it ended up leading me down the completely wrong path.

The biggest issue is that even though the docs are for CLIENT libraries, a lot of the instructions assume that you'll only ever run scrips locally so it's been a real trip trying to work out how to build a storyblok webhook that enriches algolia indexes with analytics data. Learned a lot, but definitely not from the docs.

m-shum avatar Jan 14 '25 20:01 m-shum