DefaultCredentialsError for running query in Jupyter notebook

Viewed 132

I am following this Google cloud tutorial. Running codes in Jupyter notebook. Step 5 is fine and when I run step 7 I get this error:

DefaultCredentialsError                   Traceback (most recent call last)
<ipython-input-8-a1696640ccb4> in <module>
----> 1 get_ipython().run_cell_magic('bigquery', '', 'SELECT\n    source_year AS year,\n    COUNT(is_male) AS birth_count\nFROM `bigquery-public-data.samples.natality`\nGROUP BY year\nORDER BY year DESC\nLIMIT 15\n')

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2379             with self.builtin_trap:
   2380                 args = (magic_arg_s, cell)
-> 2381                 result = fn(*args, **kwargs)
   2382             return result
   2383 
1 Answers

The DefaultCredentialsError means that the Application Default Credentials (ADC) that the client library is trying to use for authentication are not properly set.

You may have missed the step in the section Setting up a local Jupyter environment with the link for Getting Started with Authentication.

In short, you need to set your default credentials by running a cell with the following code.

export GOOGLE_APPLICATION_CREDENTIALS="path/to/key.json"

Where your path points to a valid service account key file.

You might also be able to set default credentials instead with the following in a cell if you have the gcloud sdk installed locally:

gcloud auth application-default login
Related