3

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

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

downloaded key.json from GCP. that was my problem. I copied the contents from the terminal. big no-no. Thanks
You can also pass credentials directly as parameters. ``` const {BigQuery} = require('@google-cloud/bigquery'); const options = { credentials: <content of key.json file>, // object is expected projectId: 'my_project', }; ```
5

You can also pass credentials directly as parameters.

const {BigQuery} = require('@google-cloud/bigquery'); 

const bigQuery = new BigQuery({ 
  projectId: "your-prject-id",
  credentials: {...}, // content of json file 
}); 

Thanks to @MahendraPatel comment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.