ConfusedNoob
ConfusedNoob

Reputation: 10196

How to authenticate to BigQuery using the Google Cloud SDK and API Keys

I can find lots of resources that tell me how to authenticate with Google Cloud services using a key file... but if I can't use files (long story, don't ask) how can I use the Google Cloud SDK to authenticate with an API Key?

I have generated an API Key in the Console's "APIs & Services" section but can't find any code examples showing how to use these with Google's BigQuery service for example:

// This is from the samples - but how can I pass in the API key instead??
const storage = new Storage({projectId, keyFilename});

Upvotes: 0

Views: 1699

Answers (2)

John Hanley
John Hanley

Reputation: 81454

BigQuery does not support API Key authorization. You must use an OAuth 2.0 Access Token. Nothing else is supported.

If your code is running in Google Cloud, you can use the service account assigned to the service. This method does not require a secrets file.

If your code is running outside of Google Cloud, you will need to use a service account JSON key file OR use user credentials OR user credentials that impersonate a service account. Each of these methods requires a secrets file.

[UPDATE 2021-20-17]

Based upon the comment below the OP is coding in JavaScript and deploying on Cloudflare Workers.

The Google Cloud Node.js Auth library supports loading service account JSON key material from an environment variable. This link provides example code:

Loading credentials from environment variables

Cloudflare Workers support storing configuration data via wrangler. The Cloudflare Dashboard supports adding environment variables. This link provides details on Cloudflare Environment Variables:

Adding environment variables via wrangler

Given that a service account JSON key material are secrets, I recommend using wrangler secrets instead of environment variables. Secrets are added using the CLI: wrangler secret put. The secret can then be accessed in JavaScript as an environment variable.

Adding secrets via wrangler

Wrangler Secret Put

Upvotes: 4

Aadesh kale
Aadesh kale

Reputation: 248

You can use application default credentials for the local development and testing for Prod needs to use a service account with appropriate permissions

Upvotes: -1

Related Questions