keyFilename from JSON string instead of file
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.
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:
- Make sure that the credentials line has the PARSED version of the JSON:
credentials: JSON.parse(process.env.GOOGLE_KEY) - In the
.envfile, 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
credentialsobject 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
credentialsobject 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
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',
},
})
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.
Thanks!!!! π
It's the second half of 2024 and it's still too hard to find this information.
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.
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 π )
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.