DefaultCredentialsError: Setting GOOGLE_APPLICATION_CREDENTIALS for BigQuery Python CLI

When trying to connect to Bigquery from your Python program, you may encounter the below error message:

DefaultCredentialsErrror: Could not automatically determine credentials. 
Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials 
and re-run the application. For more information, please see..

To login with your personal user account:

Install the Google Cloud SDK, Copy paste the below commands in sequence in your command prompt / putty window and follow the friendly prompts

gcloud init

This command will initialize the config file and set you up for the first time, when promted Choose "1 - Reinitialize" and follow the prompts. Once done, run the below command

gcloud auth application-default login

It will throw an URL, copy and paste that url into your chrome browser, follow the prompts and copy the generated token and paste it back in the terminal and hit enter. You should now be authenticated. You can go back to your python program and execute it, you should not face the auth error message anymore

To Login with a Service account JSON file:

Set the environmental variable directly in your python code. you can use the following code:

import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path_to_json_file'

Paste these lines into your program and you should be all set.

Alternatively, you can initialize this variable outside of the python shell:

export GOOGLE_APPLICATION_CREDENTIALS='/path/to/your/client_secret.json'

or directly initialize it in the .bashrc profile:

GOOGLE_APPLICATION_CREDENTIALS="/full/path/to/your/client_secret.json"

nVector

posted on 11 May 20

Enjoy great content like this and a lot more !

Signup for a free account to write a post / comment / upvote posts. Its simple and takes less than 5 seconds