Skip to content

Commit 4cb270a

Browse files
committed
Describing how to create credentials explicitly in auth doc.
Also moving the from_service_account_* factory descriptions into the newly added sections.
1 parent b4a8d75 commit 4cb270a

File tree

1 file changed

+80
-13
lines changed

1 file changed

+80
-13
lines changed

docs/gcloud-auth.rst

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ for interacting with an API. For example:
4040
from gcloud import datastore
4141
client = datastore.Client()
4242
43-
Passing no arguments at all will "just work" if you've following the
43+
Passing no arguments at all will "just work" if you've followed the
4444
instructions in the :ref:`Overview`. The credentials are inferred from your
4545
local environment by using Google `Application Default Credentials`_.
4646

@@ -50,38 +50,105 @@ Credential Discovery Precedence
5050
-------------------------------
5151

5252
When loading the `Application Default Credentials`_, the library will check
53-
properties of your local environment in the following order
53+
properties of your local environment in the following order:
5454

5555
#. Application running in Google App Engine
5656
#. JSON or PKCS12/P12 keyfile pointed to by
5757
``GOOGLE_APPLICATION_CREDENTIALS`` environment variable
5858
#. Credentials provided by the Google Cloud SDK (via ``gcloud auth login``)
5959
#. Application running in Google Compute Engine
6060

61-
Loading Credentials Explicitly
62-
------------------------------
61+
Explicit Credentials
62+
====================
6363

64-
In addition, the
64+
The Application Default Credentials discussed above can be useful
65+
if your code needs to run in many different environments or
66+
if you just don't want authentication to be a focus in your code.
67+
68+
However, if your code will only run in one place, you may want to
69+
use an explicit set of credentials suited to your environment.
70+
Or, you may want to use two separate accounts
71+
to simultaneously access data from different projects.
72+
73+
After creating a
74+
:class:`Credentials <oauth2client.client.Credentials>` object specific to
75+
your environment, you can pass it directly to a
76+
:class:`Client <gcloud.client.Client>`:
77+
78+
.. code:: python
79+
80+
client = Client(credentials=credentials)
81+
82+
Google App Engine Environment
83+
-----------------------------
84+
85+
To create :class:`credentials <oauth2client.appengine.AppAssertionCredentials>`
86+
just for Google App Engine:
87+
88+
.. code:: python
89+
90+
from oauth2client.appengine import AppAssertionCredentials
91+
credentials = AppAssertionCredentials([])
92+
93+
Google Compute Engine Environment
94+
---------------------------------
95+
96+
To create :class:`credentials <oauth2client.gce.AppAssertionCredentials>`
97+
just for Google Compute Engine:
98+
99+
.. code:: python
100+
101+
from oauth2client.gce import AppAssertionCredentials
102+
credentials = AppAssertionCredentials([])
103+
104+
Service Accounts
105+
----------------
106+
107+
A `service account`_ can be used with both a JSON keyfile and
108+
a PKCS12/P12 keyfile.
109+
110+
Directly creating ``credentials`` in `oauth2client`_ for a service
111+
account is a rather complex process, so as a convenience, the
65112
:meth:`from_service_account_json() <gcloud.client.Client.from_service_account_json>`
66113
and
67114
:meth:`from_service_account_p12() <gcloud.client.Client.from_service_account_p12>`
68-
factories can be used if you know the specific type of credentials you'd
69-
like to use.
115+
factories are provided to create a :class:`Client <gcloud.client.Client>` with
116+
service account credentials.
117+
118+
.. _oauth2client: http://oauth2client.readthedocs.org/en/latest/
119+
120+
For example, with a JSON keyfile:
70121

71122
.. code:: python
72123
73124
client = Client.from_service_account_json('/path/to/keyfile.json')
74125
75126
.. tip::
76127

77-
Unless you have an explicit reason to use a PKCS12 key for your
128+
Unless you have a specific reason to use a PKCS12/P12 key for your
78129
service account, we recommend using a JSON key.
79130

80-
Finally, if you are **familiar** with the `oauth2client`_ library, you can
81-
create a ``credentials`` object and pass it directly:
131+
User Accounts (3-legged OAuth 2.0) with refresh token
132+
-----------------------------------------------------
82133

83-
.. code:: python
134+
The majority of cases are intended to authenticate machines or
135+
workers rather than actual user accounts. However, it's also
136+
possible to call Google Cloud APIs with a user account via
137+
`OAuth 2.0`_.
84138

85-
client = Client(credentials=credentials)
139+
.. _OAuth 2.0: https://developers.google.com/identity/protocols/OAuth2
86140

87-
.. _oauth2client: http://oauth2client.readthedocs.org/en/latest/
141+
.. tip::
142+
143+
A production application should **use a service account**, but you
144+
may wish to use your own personal user account when first getting
145+
started with the ``gcloud-python`` library.
146+
147+
This is only supported via Application Default Credentials using
148+
``gcloud auth login`` as mentioned above. To create these
149+
credentials directly:
150+
151+
.. code:: python
152+
153+
from oauth2client.client import GoogleCredentials
154+
credentials = GoogleCredentials.get_application_default()

0 commit comments

Comments
 (0)