Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass default credentials to BigQuery using Nodejs

I am trying to create a new table in BigQuery. I have followed these instructions https://codelabs.developers.google.com/codelabs/cloud-bigquery-nodejs/index.html?index=..%2F..index#9 and have my user and roles defined properly.

I created a node project, installed the google dependencies and have the following code:

 const {BigQuery} = require('@google-cloud/bigquery');
 const bigquery = new BigQuery({
   projectId: 'myproject-develop-3fcb6',
  private_key_id: "11111111111",
  client_email: "myuser-bigquery-sa@myproject-develop-3fcb6.iam.gserviceaccount.com",
  client_id: "212111112",
  });

This is how im creating my dataset and table:

 module.exports = {
  createTable: ({ datasetId, tableId, schema, partitionBy}) => {
    const options = { schema };
    if (partitionBy) {
     options.timePartitioning = {
     field: partitionBy
   };
  }

  return new Promise((resolve, reject) => {
    resolve();
     bigquery
    .dataset(datasetId)
    .createTable(tableId, options)
    .then(results => resolve(results[0]))
    .catch(err => {
      handleError(err);
      reject(err);
    });
   });
  },
 };

When i run my createTableFunction and pass in the data set name, table name, schema I get the following error immediately

ERROR: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.

How do I pass my default credentials to BigQuery so i can perform CRUD operations in node.js? Thanks

like image 619
BoundForGlory Avatar asked Jun 19 '26 15:06

BoundForGlory


1 Answers

In the tutorial that you mentioned, this gcloud command creates a key.json:

   gcloud iam service-accounts keys create ~/key.json --iam-account  my-bigquery-sa@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com

Then you can use the following code:

 // Create a BigQuery client explicitly using service account credentials.
 // by specifying the private key file.
 const {BigQuery} = require('@google-cloud/bigquery');

 const options = {
   keyFilename: 'path/to/key.json',
   projectId: 'my_project',
 };

const bigquery = new BigQuery(options);

Authenticating With a Service Account Key File

I do not know where are you running your code, but in the tutorial is a line where you set the env variable therefore you do not need to authenticate using the key.json file in your code:

   export GOOGLE_APPLICATION_CREDENTIALS="/home/${USER}/key.json"

GCP client libraries use a strategy called Application Default Credentials (ADC) to find your application's credentials. When your code uses a client library, the strategy checks for your credentials in the following order:

First, ADC checks to see if the environment variable GOOGLE_APPLICATION_CREDENTIALS is set. If the variable is set, ADC uses the service account file that the variable points to. The next section describes how to set the environment variable.

If the environment variable isn't set, ADC uses the default service account that Compute Engine, Kubernetes Engine, Cloud Run, App Engine, and Cloud Functions provide, for applications that run on those services.

If ADC can't use either of the above credentials, an error occurs.

like image 176
marian.vladoi Avatar answered Jun 23 '26 16:06

marian.vladoi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!