Skip to content

Conversation

@nw
Copy link

@nw nw commented Oct 2, 2016

This patch helps to streamline authentication with google-cloud modules.

Example:

const bq = require('@google-cloud/bigquery')(require('./gcloud_key.json'))

gcloud_key.json is a non modified exported key from IAM service accounts.

This patch normalizes the name of project_id found in the keyfile to projectId to work with google-auto-auth library.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Oct 2, 2016
@coveralls
Copy link

Coverage Status

Changes Unknown when pulling ce67f70 on nw:nw/fix-normalize-projectId into * on GoogleCloudPlatform:master*.

@stephenplusplus
Copy link
Contributor

Thank you for this! It's very interestingly-timed, because we're working on getting the projectId in a similar way with one of our dependencies: googleapis/google-auth-library-nodejs#98. I think we should wait for that, since it will work for all of the various ways we accept credentials-- keyfile, environment variable, and a credentials object-- and we won't have to special-case this scenario.

const bq = require('@google-cloud/bigquery')(require('./gcloud_key.json'))

This confuses me a bit because when a user provides a parsed key file, we accept it under the credentials key like this:

const bq = require('@google-cloud/bigquery')({
  credentials: require('./keyfile.json')
})

Was your example a typo, or does it work without using the credentials key as well?

@nw
Copy link
Author

nw commented Oct 3, 2016

@stephenplusplus Yeah I saw the discussion about google-auth just after putting this PR.

The example is correct. The json key files have all the credentials needed with the exception of normalizing the projectId.

Maybe this example is clearer

const creds = require('./keyfile.json')
const bq = require('@google-cloud/bigquery')(creds)

@stephenplusplus
Copy link
Contributor

stephenplusplus commented Oct 3, 2016

When I try that, I get:

var creds = require('/Users/stephen/dev/keyfile.json')
creds.projectId = creds.project_id
var bigquery = require('@google-cloud/bigquery')(creds)
Error: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information.
    at /Users/stephen/dev/play/gcloud-1475518276/node_modules/google-auth-library/lib/auth/googleauth.js:145:21

To fix:

var creds = require('/Users/stephen/dev/keyfile.json')
var bigquery = require('@google-cloud/bigquery')({
  projectId: creds.project_id,
  credentials: creds
})

I'm going to close since we'll wait out googleapis/google-auth-library-nodejs#98, but still thank you very much for the help. If you find anything else you think can be improved, please let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants