Skip to content

Commit 06dc9a2

Browse files
authored
Add optional colab specific ADC (#68)
1 parent 5efe5b8 commit 06dc9a2

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

pydata_google_auth/auth.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ def default(
157157
return credentials, None
158158

159159

160+
def _ensure_application_default_credentials_in_colab_environment():
161+
# This is a special handling for google colab environment where we want to
162+
# use the colab specific authentication flow
163+
# https://github.com/googlecolab/colabtools/blob/3c8772efd332289e1c6d1204826b0915d22b5b95/google/colab/auth.py#L209
164+
try:
165+
from google.colab import auth
166+
167+
auth.authenticate_user()
168+
except (ModuleNotFoundError, ImportError):
169+
pass
170+
171+
160172
def get_application_default_credentials(scopes):
161173
"""
162174
This method tries to retrieve the "default application credentials".
@@ -178,6 +190,8 @@ def get_application_default_credentials(scopes):
178190
have access to the project (project_id) on BigQuery.
179191
"""
180192

193+
_ensure_application_default_credentials_in_colab_environment()
194+
181195
try:
182196
credentials, project = google.auth.default(scopes=scopes)
183197
except (google.auth.exceptions.DefaultCredentialsError, IOError) as exc:

0 commit comments

Comments
 (0)