@@ -146,7 +146,8 @@ def build(serviceName,
146
146
discoveryServiceUrl = DISCOVERY_URI ,
147
147
developerKey = None ,
148
148
model = None ,
149
- requestBuilder = HttpRequest ):
149
+ requestBuilder = HttpRequest ,
150
+ credentials = None ):
150
151
"""Construct a Resource for interacting with an API.
151
152
152
153
Construct a Resource object for interacting with an API. The serviceName and
@@ -166,6 +167,8 @@ def build(serviceName,
166
167
model: googleapiclient.Model, converts to and from the wire format.
167
168
requestBuilder: googleapiclient.http.HttpRequest, encapsulator for an HTTP
168
169
request.
170
+ credentials: oauth2client.Credentials, credentials to be used for
171
+ authentication.
169
172
170
173
Returns:
171
174
A Resource object with methods for interacting with the service.
@@ -204,7 +207,8 @@ def build(serviceName,
204
207
raise InvalidJsonError ()
205
208
206
209
return build_from_document (content , base = discoveryServiceUrl , http = http ,
207
- developerKey = developerKey , model = model , requestBuilder = requestBuilder )
210
+ developerKey = developerKey , model = model , requestBuilder = requestBuilder ,
211
+ credentials = credentials )
208
212
209
213
210
214
@positional (1 )
@@ -215,7 +219,8 @@ def build_from_document(
215
219
http = None ,
216
220
developerKey = None ,
217
221
model = None ,
218
- requestBuilder = HttpRequest ):
222
+ requestBuilder = HttpRequest ,
223
+ credentials = None ):
219
224
"""Create a Resource for interacting with an API.
220
225
221
226
Same as `build()`, but constructs the Resource object from a discovery
@@ -236,6 +241,7 @@ def build_from_document(
236
241
model: Model class instance that serializes and de-serializes requests and
237
242
responses.
238
243
requestBuilder: Takes an http request and packages it up to be executed.
244
+ credentials: object, credentials to be used for authentication.
239
245
240
246
Returns:
241
247
A Resource object with methods for interacting with the service.
@@ -249,6 +255,27 @@ def build_from_document(
249
255
base = urlparse .urljoin (service ['rootUrl' ], service ['servicePath' ])
250
256
schema = Schemas (service )
251
257
258
+ if credentials :
259
+ # If credentials were passed in, we could have two cases:
260
+ # 1. the scopes were specified, in which case the given credentials
261
+ # are used for authorizing the http;
262
+ # 2. the scopes were not provided (meaning the Default Credentials are
263
+ # to be used). In this case, the Default Credentials are built and
264
+ # used instead of the original credentials. If there are no scopes
265
+ # found (meaning the given service requires no authentication), there is
266
+ # no authorization of the http.
267
+ if credentials .create_scoped_required ():
268
+ scopes = service .get ('auth' , {}).get ('oauth2' , {}).get ('scopes' , {})
269
+ if scopes :
270
+ credentials = credentials .create_scoped (scopes .keys ())
271
+ else :
272
+ # No need to authorize the http object
273
+ # if the service does not require authentication.
274
+ credentials = None
275
+
276
+ if credentials :
277
+ http = credentials .authorize (http )
278
+
252
279
if model is None :
253
280
features = service .get ('features' , [])
254
281
model = JsonModel ('dataWrapper' in features )
0 commit comments