Skip to content

Commit 5fd1775

Browse files
committed
added more documentation
1 parent 64134ac commit 5fd1775

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

doc/source/whatsnew/v0.19.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ Google BigQuery Enhancements
361361
Other enhancements
362362
^^^^^^^^^^^^^^^^^^
363363

364-
- The ``.get_credentials()`` method of ``GbqConnector`` can now first try to fetch the default credentials for Google Compute Engine without the need to run ``OAuth2WebServerFlow`` - if private_key is not provided (:issue:`13577`)
364+
- The ``.get_credentials()`` method of ``GbqConnector`` can now first try to fetch the application default credentials (https://developers.google.com/identity/protocols/application-default-credentials). This is only valid if the parameter ``private_key`` is not provided. Using this functionality, the ``OAuth2WebServerFlow`` call can be skipped (:issue:`13577`).
365365

366366
- The ``.tz_localize()`` method of ``DatetimeIndex`` and ``Timestamp`` has gained the ``errors`` keyword, so you can potentially coerce nonexistent timestamps to ``NaT``. The default behaviour remains to raising a ``NonExistentTimeError`` (:issue:`13057`)
367367
- ``pd.to_numeric()`` now accepts a ``downcast`` parameter, which will downcast the data if possible to smallest specified numerical dtype (:issue:`13352`)

pandas/io/gbq.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ def _test_google_api_imports():
5252
except:
5353
from apiclient.discovery import build # noqa
5454
from apiclient.errors import HttpError # noqa
55+
from oauth2client.client import GoogleCredentials # noqa
5556
from oauth2client.client import AccessTokenRefreshError # noqa
57+
from oauth2client.client import HttpAccessTokenRefreshError # noqa
58+
from oauth2client.client import ApplicationDefaultCredentialsError # noqa
5659
from oauth2client.client import OAuth2WebServerFlow # noqa
5760
from oauth2client.file import Storage # noqa
5861
from oauth2client.tools import run_flow, argparser # noqa
@@ -168,25 +171,37 @@ def get_credentials(self):
168171

169172
def get_application_default_credentials(self):
170173
"""
171-
This method tries to retrieve the "default application credentials"
172-
Could be useful for running code on Google Cloud Platform
174+
This method tries to retrieve the "default application credentials".
175+
This could be useful for running code on Google Cloud Platform.
176+
177+
.. versionadded:: 0.19.0
178+
179+
Parameters
180+
----------
181+
None
182+
183+
Returns
184+
-------
185+
- GoogleCredentials,
186+
If the default application credentials can be retrieved
187+
from the environment. The retrieved credentials should also
188+
have access to the project (self.project_id) on BigQuery.
189+
- OR None,
190+
If default application credentials can not be retrieved
191+
from the environment. Or, the retrieved credentials do not
192+
have access to the project (self.project_id) on BigQuery.
173193
"""
174-
oauth2client_library_imported = False
175194
try:
176-
from oauth2client.client import GoogleCredentials
177-
from oauth2client.client import AccessTokenRefreshError
178-
from oauth2client.client import HttpAccessTokenRefreshError
179-
from oauth2client.client import ApplicationDefaultCredentialsError
180-
oauth2client_library_imported = True
181195
from googleapiclient.discovery import build
182196
from googleapiclient.errors import HttpError
183197
except ImportError:
184-
if not oauth2client_library_imported:
185-
return None
186198
from apiclient.discovery import build
187199
from apiclient.errors import HttpError
200+
from oauth2client.client import GoogleCredentials
201+
from oauth2client.client import AccessTokenRefreshError
202+
from oauth2client.client import HttpAccessTokenRefreshError
203+
from oauth2client.client import ApplicationDefaultCredentialsError
188204

189-
credentials = None
190205
try:
191206
credentials = GoogleCredentials.get_application_default()
192207
except ApplicationDefaultCredentialsError:
@@ -198,10 +213,10 @@ def get_application_default_credentials(self):
198213
job_data = {'configuration': {'query': {'query': 'SELECT 1'}}}
199214
try:
200215
jobs.insert(projectId=self.project_id, body=job_data).execute()
216+
return credentials
201217
except (HttpAccessTokenRefreshError, AccessTokenRefreshError,
202218
HttpError, TypeError):
203219
return None
204-
return credentials
205220

206221
def get_user_account_credentials(self):
207222
from oauth2client.client import OAuth2WebServerFlow

0 commit comments

Comments
 (0)