@@ -32,56 +32,132 @@ Overview
32
32
Client-Provided Authentication
33
33
==============================
34
34
35
- Every package uses a :class: `Client <gcloud.client.Client> ` as a base
36
- for interacting with an API. For example:
35
+ Every package uses a :class: `Client <gcloud.client.Client> `
36
+ as a base for interacting with an API.
37
+ For example:
37
38
38
39
.. code-block :: python
39
40
40
41
from gcloud import datastore
41
42
client = datastore.Client()
42
43
43
- Passing no arguments at all will "just work" if you've following the
44
- instructions in the :ref: `Overview `. The credentials are inferred from your
45
- local environment by using Google `Application Default Credentials `_.
44
+ Passing no arguments at all will "just work" if you've followed the
45
+ instructions in the :ref: `Overview `.
46
+ The credentials are inferred from your local environment by using
47
+ Google `Application Default Credentials `_.
46
48
47
49
.. _Application Default Credentials : https://developers.google.com/identity/protocols/application-default-credentials
48
50
49
51
Credential Discovery Precedence
50
52
-------------------------------
51
53
52
- When loading the `Application Default Credentials `_, the library will check
53
- properties of your local environment in the following order
54
+ When loading the `Application Default Credentials `_,
55
+ the library will check properties of your local environment
56
+ in the following order:
54
57
55
58
#. Application running in Google App Engine
56
59
#. JSON or PKCS12/P12 keyfile pointed to by
57
60
``GOOGLE_APPLICATION_CREDENTIALS `` environment variable
58
61
#. Credentials provided by the Google Cloud SDK (via ``gcloud auth login ``)
59
62
#. Application running in Google Compute Engine
60
63
61
- Loading Credentials Explicitly
62
- ------------------------------
64
+ Explicit Credentials
65
+ ====================
63
66
64
- In addition, the
67
+ The Application Default Credentials discussed above can be useful
68
+ if your code needs to run in many different environments or
69
+ if you just don't want authentication to be a focus in your code.
70
+
71
+ However, you may want to be explicit because
72
+
73
+ * your code will only run in one place
74
+ * you may have code which needs to be run as a specific service account
75
+ every time (rather than with the locally inferred credentials)
76
+ * you may want to use two separate accounts to simultaneously access data
77
+ from different projects
78
+
79
+ In these situations, you can create an explicit
80
+ :class: `Credentials <oauth2client.client.Credentials> ` object suited to your
81
+ environment.
82
+ After creation,
83
+ you can pass it directly to a :class: `Client <gcloud.client.Client> `:
84
+
85
+ .. code :: python
86
+
87
+ client = Client(credentials = credentials)
88
+
89
+ Google App Engine Environment
90
+ -----------------------------
91
+
92
+ To create :class: `credentials <oauth2client.appengine.AppAssertionCredentials> `
93
+ just for Google App Engine:
94
+
95
+ .. code :: python
96
+
97
+ from oauth2client.appengine import AppAssertionCredentials
98
+ credentials = AppAssertionCredentials([])
99
+
100
+ Google Compute Engine Environment
101
+ ---------------------------------
102
+
103
+ To create :class: `credentials <oauth2client.gce.AppAssertionCredentials> `
104
+ just for Google Compute Engine:
105
+
106
+ .. code :: python
107
+
108
+ from oauth2client.gce import AppAssertionCredentials
109
+ credentials = AppAssertionCredentials([])
110
+
111
+ Service Accounts
112
+ ----------------
113
+
114
+ A `service account `_ can be used with both a JSON keyfile and
115
+ a PKCS12/P12 keyfile.
116
+
117
+ Directly creating ``credentials `` in `oauth2client `_ for a service
118
+ account is a rather complex process,
119
+ so as a convenience, the
65
120
:meth: `from_service_account_json() <gcloud.client.Client.from_service_account_json> `
66
121
and
67
122
: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.
123
+ factories are provided to create a :class: `Client <gcloud.client.Client> ` with
124
+ service account credentials.
125
+
126
+ .. _oauth2client : http://oauth2client.readthedocs.org/en/latest/
127
+
128
+ For example, with a JSON keyfile:
70
129
71
130
.. code :: python
72
131
73
132
client = Client.from_service_account_json(' /path/to/keyfile.json' )
74
133
75
134
.. tip ::
76
135
77
- Unless you have an explicit reason to use a PKCS12 key for your
78
- service account, we recommend using a JSON key.
136
+ Unless you have a specific reason to use a PKCS12/P12 key for your
137
+ service account,
138
+ we recommend using a JSON key.
79
139
80
- Finally, if you are ** familiar ** with the ` oauth2client `_ library, you can
81
- create a `` credentials `` object and pass it directly:
140
+ User Accounts (3-legged OAuth 2.0) with a refresh token
141
+ -------------------------------------------------------
82
142
83
- .. code :: python
143
+ The majority of cases are intended to authenticate machines or
144
+ workers rather than actual user accounts. However, it's also
145
+ possible to call Google Cloud APIs with a user account via
146
+ `OAuth 2.0 `_.
84
147
85
- client = Client( credentials = credentials)
148
+ .. _ OAuth 2.0: https://developers.google.com/identity/protocols/OAuth2
86
149
87
- .. _oauth2client : http://oauth2client.readthedocs.org/en/latest/
150
+ .. tip ::
151
+
152
+ A production application should **use a service account **,
153
+ but you may wish to use your own personal user account when first
154
+ getting started with the ``gcloud-python `` library.
155
+
156
+ This is only supported via Application Default Credentials using
157
+ ``gcloud auth login `` as mentioned above.
158
+ To create these credentials directly:
159
+
160
+ .. code :: python
161
+
162
+ from oauth2client.client import GoogleCredentials
163
+ credentials = GoogleCredentials.get_application_default()
0 commit comments