Skip to content

Commit 7439771

Browse files
authored
DOC: Add how-to guide for authentication (#183)
* Clarifies order of authentication methods. * Updates links to external resources.
1 parent 0816668 commit 7439771

File tree

5 files changed

+81
-75
lines changed

5 files changed

+81
-75
lines changed

docs/source/conf.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,12 @@
359359
# texinfo_no_detailmenu = False
360360

361361

362-
# Example configuration for intersphinx: refer to the Python standard library.
363-
intersphinx_mapping = {'https://docs.python.org/': None}
362+
# Configuration for intersphinx:
363+
intersphinx_mapping = {
364+
'https://docs.python.org/': None,
365+
'https://pandas.pydata.org/pandas-docs/stable/': None,
366+
'https://google-auth.readthedocs.io/en/latest/': None,
367+
}
364368

365369
extlinks = {'issue': ('https://github.com/pydata/pandas-gbq/issues/%s',
366370
'GH#'),

docs/source/howto/authentication.rst

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Authentication
2+
==============
3+
4+
pandas-gbq `authenticates with the Google BigQuery service
5+
<https://cloud.google.com/bigquery/docs/authentication/>`_ via OAuth 2.0.
6+
7+
.. _authentication:
8+
9+
10+
Authentication with a Service Account
11+
--------------------------------------
12+
13+
Using service account credentials is particularly useful when working on
14+
remote servers without access to user input.
15+
16+
Create a service account key via the `service account key creation page
17+
<https://console.cloud.google.com/apis/credentials/serviceaccountkey>`_ in
18+
the Google Cloud Platform Console. Select the JSON key type and download the
19+
key file.
20+
21+
To use service account credentials, set the ``private_key`` parameter to one
22+
of:
23+
24+
* A file path to the JSON file.
25+
* A string containing the JSON file contents.
26+
27+
See the `Getting started with authentication on Google Cloud Platform
28+
<https://cloud.google.com/docs/authentication/getting-started>`_ guide for
29+
more information on service accounts.
30+
31+
Default Authentication Methods
32+
------------------------------
33+
34+
If the ``private_key`` parameter is ``None``, pandas-gbq tries the following
35+
authentication methods:
36+
37+
1. Application Default Credentials via the :func:`google.auth.default`
38+
function.
39+
40+
.. note::
41+
42+
If pandas-gbq can obtain default credentials but those credentials
43+
cannot be used to query BigQuery, pandas-gbq will also try obtaining
44+
user account credentials.
45+
46+
A common problem with default credentials when running on Google
47+
Compute Engine is that the VM does not have sufficient scopes to query
48+
BigQuery.
49+
50+
2. User account credentials.
51+
52+
pandas-gbq loads cached credentials from a hidden user folder on the
53+
operating system. Override the location of the cached user credentials
54+
by setting the ``PANDAS_GBQ_CREDENTIALS_FILE`` environment variable.
55+
56+
If pandas-gbq does not find cached credentials, it opens a browser window
57+
asking for you to authenticate to your BigQuery account using the product
58+
name ``pandas GBQ``.
59+
60+
Additional information on the user credentails authentication mechanism
61+
can be found `here
62+
<https://developers.google.com/identity/protocols/OAuth2#clientside/>`__.

docs/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Contents:
2626

2727
install.rst
2828
intro.rst
29+
howto/authentication.rst
2930
reading.rst
3031
writing.rst
3132
tables.rst

docs/source/intro.rst

-39
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,3 @@ more verbose logs, you can do something like:
4141
logger = logging.getLogger('pandas_gbq')
4242
logger.setLevel(logging.DEBUG)
4343
logger.addHandler(logging.StreamHandler(stream=sys.stdout))
44-
45-
46-
.. _authentication:
47-
48-
Authentication
49-
''''''''''''''
50-
51-
Authentication to the Google ``BigQuery`` service via ``OAuth 2.0``
52-
is possible with either user or service account credentials.
53-
54-
Authentication via user account credentials is as simple as following the prompts in a browser window
55-
which will automatically open for you. You authenticate to the specified
56-
``BigQuery`` account using the product name ``pandas GBQ``.
57-
The remote authentication is supported via the ``auth_local_webserver`` in ``read_gbq``. By default,
58-
account credentials are stored in an application-specific hidden user folder on the operating system. You
59-
can override the default credentials location via the ``PANDAS_GBQ_CREDENTIALS_FILE`` environment variable.
60-
Additional information on the authentication mechanism can be found
61-
`here <https://developers.google.com/identity/protocols/OAuth2#clientside/>`__.
62-
63-
Authentication via service account credentials is possible through the `'private_key'` parameter. This method
64-
is particularly useful when working on remote servers (eg. Jupyter Notebooks on remote host).
65-
Additional information on service accounts can be found
66-
`here <https://developers.google.com/identity/protocols/OAuth2#serviceaccount>`__.
67-
68-
Authentication via ``application default credentials`` is also possible, but only valid
69-
if the parameter ``private_key`` is not provided. This method requires that the
70-
credentials can be fetched from the development environment. Otherwise, the OAuth2
71-
client-side authentication is used. Additional information can be found on
72-
`application default credentials <https://developers.google.com/identity/protocols/application-default-credentials>`__.
73-
74-
.. note::
75-
76-
The `'private_key'` parameter can be set to either the file path of the service account key
77-
in JSON format, or key contents of the service account key in JSON format.
78-
79-
.. note::
80-
81-
A private key can be obtained from the Google developers console by clicking
82-
`here <https://console.developers.google.com/permissions/serviceaccounts>`__. Use JSON key type.

pandas_gbq/gbq.py

+12-34
Original file line numberDiff line numberDiff line change
@@ -476,23 +476,12 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None,
476476
The main method a user calls to execute a Query in Google BigQuery
477477
and read results into a pandas DataFrame.
478478
479-
The Google Cloud library is used.
480-
Documentation is available `here
481-
<https://googlecloudplatform.github.io/google-cloud-python/stable/>`__
479+
This method uses the Google Cloud client library to make requests to
480+
Google BigQuery, documented `here
481+
<https://google-cloud-python.readthedocs.io/en/latest/bigquery/usage.html>`__.
482482
483-
Authentication to the Google BigQuery service is via OAuth 2.0.
484-
485-
- If "private_key" is not provided:
486-
487-
By default "application default credentials" are used.
488-
489-
If default application credentials are not found or are restrictive,
490-
user account credentials are used. In this case, you will be asked to
491-
grant permissions for product name 'pandas GBQ'.
492-
493-
- If "private_key" is provided:
494-
495-
Service account credentials will be used to authenticate.
483+
See the :ref:`How to authenticate with Google BigQuery <authentication>`
484+
guide for authentication instructions.
496485
497486
Parameters
498487
----------
@@ -612,29 +601,18 @@ def to_gbq(dataframe, destination_table, project_id=None, chunksize=None,
612601
The main method a user calls to export pandas DataFrame contents to
613602
Google BigQuery table.
614603
615-
Google BigQuery API Client Library v2 for Python is used.
616-
Documentation is available `here
617-
<https://developers.google.com/api-client-library/python/apis/bigquery/v2>`__
618-
619-
Authentication to the Google BigQuery service is via OAuth 2.0.
620-
621-
- If "private_key" is not provided:
622-
623-
By default "application default credentials" are used.
624-
625-
If default application credentials are not found or are restrictive,
626-
user account credentials are used. In this case, you will be asked to
627-
grant permissions for product name 'pandas GBQ'.
628-
629-
- If "private_key" is provided:
604+
This method uses the Google Cloud client library to make requests to
605+
Google BigQuery, documented `here
606+
<https://google-cloud-python.readthedocs.io/en/latest/bigquery/usage.html>`__.
630607
631-
Service account credentials will be used to authenticate.
608+
See the :ref:`How to authenticate with Google BigQuery <authentication>`
609+
guide for authentication instructions.
632610
633611
Parameters
634612
----------
635-
dataframe : DataFrame
613+
dataframe : pandas.DataFrame
636614
DataFrame to be written
637-
destination_table : string
615+
destination_table : str
638616
Name of table to be written, in the form 'dataset.tablename'
639617
project_id : str (optional when available in environment)
640618
Google BigQuery Account project ID.

0 commit comments

Comments
 (0)