We still don't avoid calling the authorize() method if the service does not
need authentication (ie, service['auth']['oauth2'] does not have 'scopes')
and the user requested the Default Credentials (ie, the user did not
specify any scopes in his credentials). This is all my code is about.


On Thu, May 29, 2014 at 11:08 AM, Craig Citro <notifications@github.com>
wrote:

> In googleapiclient/discovery.py:
>
> > @@ -249,6 +255,21 @@ def build_from_document(
> >    base = urlparse.urljoin(service['rootUrl'], service['servicePath'])
> >    schema = Schemas(service)
> >
> > +  if credentials:
> > +    should_authorize = True
> > +    if credentials.create_scoped_required():
> > +      if ('auth' in service and
> > +          'oauth2' in service['auth'] and
> > +          'scopes' in service['auth']['oauth2']):
> > +        credentials = credentials.create_scoped(
> > +            service['auth']['oauth2']['scopes'].keys())
> > +      else:
> > +        # No need to authorize the http object
> > +        # if the service does not require authentication.
> > +        should_authorize = False
> > +    if should_authorize:
> > +      http = credentials.authorize(http)
>
> So one way to go is just add the comment, but I feel like 8 lines of code
> needing a comparably large comment is code begging for some cleanup.
>
> It seems like the easiest thing to do would be to pull the "do we need to
> skip credentials because there are no scopes" bit out of this block. Here's
> an idea: if there are no scopes, how about we have create_scoped just
> return None. Then we get:
>
> if credentials.create_scoped_required():
>   scopes = service.get('auth', {}).get('oauth2', {}).get('scopes', {}).keys()
>   credentials = credentials.create_scoped(scopes)
> if credentials:
>   http = credentials.authorize(http)
>
> Now it's pretty clear: first, we settle the question of "do we need to do
> something with the default credentials", and then authorize if we have
> credentials in hand.
>
> If you don't like the return None bit, we could also have clauses in the
> first if above.
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/google/google-api-python-client/pull/4/files#r13195108>
> .
>



-- 
Orest Bolohan

Software Engineer
Cloud/TI Identity and Access Management Services
Google Inc.
Mountain View, CA

(650) 214 3458
orest@google.com