5353import uritemplate
5454
5555# Local imports
56+ from googleapiclient import _auth
5657from googleapiclient import mimeparse
5758from googleapiclient .errors import HttpError
5859from googleapiclient .errors import InvalidJsonError
@@ -197,7 +198,8 @@ def build(serviceName,
197198 model: googleapiclient.Model, converts to and from the wire format.
198199 requestBuilder: googleapiclient.http.HttpRequest, encapsulator for an HTTP
199200 request.
200- credentials: oauth2client.Credentials, credentials to be used for
201+ credentials: oauth2client.Credentials or
202+ google.auth.credentials.Credentials, credentials to be used for
201203 authentication.
202204 cache_discovery: Boolean, whether or not to cache the discovery doc.
203205 cache: googleapiclient.discovery_cache.base.CacheBase, an optional
@@ -211,15 +213,14 @@ def build(serviceName,
211213 'apiVersion' : version
212214 }
213215
214- if http is None :
215- http = httplib2 .Http ()
216+ discovery_http = http if http is not None else httplib2 .Http ()
216217
217218 for discovery_url in (discoveryServiceUrl , V2_DISCOVERY_URI ,):
218219 requested_url = uritemplate .expand (discovery_url , params )
219220
220221 try :
221- content = _retrieve_discovery_doc (requested_url , http , cache_discovery ,
222- cache )
222+ content = _retrieve_discovery_doc (
223+ requested_url , discovery_http , cache_discovery , cache )
223224 return build_from_document (content , base = discovery_url , http = http ,
224225 developerKey = developerKey , model = model , requestBuilder = requestBuilder ,
225226 credentials = credentials )
@@ -316,17 +317,16 @@ def build_from_document(
316317 model: Model class instance that serializes and de-serializes requests and
317318 responses.
318319 requestBuilder: Takes an http request and packages it up to be executed.
319- credentials: object, credentials to be used for authentication.
320+ credentials: oauth2client.Credentials or
321+ google.auth.credentials.Credentials, credentials to be used for
322+ authentication.
320323
321324 Returns:
322325 A Resource object with methods for interacting with the service.
323326 """
324327
325- if http is None :
326- http = httplib2 .Http ()
327-
328- # future is no longer used.
329- future = {}
328+ if http is not None and credentials is not None :
329+ raise ValueError ('Arguments http and credentials are mutually exclusive.' )
330330
331331 if isinstance (service , six .string_types ):
332332 service = json .loads (service )
@@ -342,31 +342,36 @@ def build_from_document(
342342 base = urljoin (service ['rootUrl' ], service ['servicePath' ])
343343 schema = Schemas (service )
344344
345- if credentials :
346- # If credentials were passed in, we could have two cases:
347- # 1. the scopes were specified, in which case the given credentials
348- # are used for authorizing the http;
349- # 2. the scopes were not provided (meaning the Application Default
350- # Credentials are to be used). In this case, the Application Default
351- # Credentials are built and used instead of the original credentials.
352- # If there are no scopes found (meaning the given service requires no
353- # authentication), there is no authorization of the http.
354- if (isinstance (credentials , GoogleCredentials ) and
355- credentials .create_scoped_required ()):
356- scopes = service .get ('auth' , {}).get ('oauth2' , {}).get ('scopes' , {})
357- if scopes :
358- credentials = credentials .create_scoped (list (scopes .keys ()))
359- else :
360- # No need to authorize the http object
361- # if the service does not require authentication.
362- credentials = None
345+ # If the http client is not specified, then we must construct an http client
346+ # to make requests. If the service has scopes, then we also need to setup
347+ # authentication.
348+ if http is None :
349+ # Does the service require scopes?
350+ scopes = list (
351+ service .get ('auth' , {}).get ('oauth2' , {}).get ('scopes' , {}).keys ())
363352
364- if credentials :
365- http = credentials .authorize (http )
353+ # If so, then the we need to setup authentication.
354+ if scopes :
355+ # If the user didn't pass in credentials, attempt to acquire application
356+ # default credentials.
357+ if credentials is None :
358+ credentials = _auth .default_credentials ()
359+
360+ # The credentials need to be scoped.
361+ credentials = _auth .with_scopes (credentials , scopes )
362+
363+ # Create an authorized http instance
364+ http = _auth .authorized_http (credentials )
365+
366+ # If the service doesn't require scopes then there is no need for
367+ # authentication.
368+ else :
369+ http = httplib2 .Http ()
366370
367371 if model is None :
368372 features = service .get ('features' , [])
369373 model = JsonModel ('dataWrapper' in features )
374+
370375 return Resource (http = http , baseUrl = base , model = model ,
371376 developerKey = developerKey , requestBuilder = requestBuilder ,
372377 resourceDesc = service , rootDesc = service , schema = schema )
0 commit comments