|
16 | 16 |
|
17 | 17 |
|
18 | 18 | def get_credentials( |
19 | | - private_key=None, project_id=None, reauth=False, auth_local_webserver=False |
| 19 | + private_key=None, |
| 20 | + project_id=None, |
| 21 | + reauth=False, |
| 22 | + auth_local_webserver=False, |
| 23 | + try_credentials=None, |
20 | 24 | ): |
| 25 | + if try_credentials is None: |
| 26 | + try_credentials = _try_credentials |
| 27 | + |
21 | 28 | if private_key: |
22 | 29 | return get_service_account_credentials(private_key) |
23 | 30 |
|
24 | 31 | # Try to retrieve Application Default Credentials |
25 | 32 | credentials, default_project = get_application_default_credentials( |
26 | | - project_id=project_id |
| 33 | + try_credentials, project_id=project_id |
27 | 34 | ) |
28 | 35 |
|
29 | 36 | if credentials: |
30 | 37 | return credentials, default_project |
31 | 38 |
|
32 | 39 | credentials = get_user_account_credentials( |
| 40 | + try_credentials, |
33 | 41 | project_id=project_id, |
34 | 42 | reauth=reauth, |
35 | 43 | auth_local_webserver=auth_local_webserver, |
@@ -79,7 +87,7 @@ def get_service_account_credentials(private_key): |
79 | 87 | ) |
80 | 88 |
|
81 | 89 |
|
82 | | -def get_application_default_credentials(project_id=None): |
| 90 | +def get_application_default_credentials(try_credentials, project_id=None): |
83 | 91 | """ |
84 | 92 | This method tries to retrieve the "default application credentials". |
85 | 93 | This could be useful for running code on Google Cloud Platform. |
@@ -111,10 +119,11 @@ def get_application_default_credentials(project_id=None): |
111 | 119 | # used with BigQuery. For example, we could be running on a GCE instance |
112 | 120 | # that does not allow the BigQuery scopes. |
113 | 121 | billing_project = project_id or default_project |
114 | | - return _try_credentials(billing_project, credentials), billing_project |
| 122 | + return try_credentials(billing_project, credentials), billing_project |
115 | 123 |
|
116 | 124 |
|
117 | 125 | def get_user_account_credentials( |
| 126 | + try_credentials, |
118 | 127 | project_id=None, |
119 | 128 | reauth=False, |
120 | 129 | auth_local_webserver=False, |
@@ -151,7 +160,9 @@ def get_user_account_credentials( |
151 | 160 | os.rename("bigquery_credentials.dat", credentials_path) |
152 | 161 |
|
153 | 162 | credentials = load_user_account_credentials( |
154 | | - project_id=project_id, credentials_path=credentials_path |
| 163 | + try_credentials, |
| 164 | + project_id=project_id, |
| 165 | + credentials_path=credentials_path, |
155 | 166 | ) |
156 | 167 |
|
157 | 168 | client_config = { |
@@ -187,7 +198,9 @@ def get_user_account_credentials( |
187 | 198 | return credentials |
188 | 199 |
|
189 | 200 |
|
190 | | -def load_user_account_credentials(project_id=None, credentials_path=None): |
| 201 | +def load_user_account_credentials( |
| 202 | + try_credentials, project_id=None, credentials_path=None |
| 203 | +): |
191 | 204 | """ |
192 | 205 | Loads user account credentials from a local file. |
193 | 206 |
|
@@ -230,7 +243,7 @@ def load_user_account_credentials(project_id=None, credentials_path=None): |
230 | 243 | request = google.auth.transport.requests.Request() |
231 | 244 | credentials.refresh(request) |
232 | 245 |
|
233 | | - return _try_credentials(project_id, credentials) |
| 246 | + return try_credentials(project_id, credentials) |
234 | 247 |
|
235 | 248 |
|
236 | 249 | def get_default_credentials_path(): |
|
0 commit comments