'Pre or Re-auth stuff...
This will attempt to avoid making any OAuth related HTTP connections or
user interactions unless it\'s needed.
Args:
http: An \'Http\' object from httplib2.
needs_auth: If the user has already tried to contact the server.
If they have, it\'s OK to prompt them. If not, we should not be asking
them for auth info--it\'s possible it\'ll suceed w/o auth, but if we have
some credentials we\'ll use them anyway.
Raises:
AuthPermanentFail: The user has requested non-interactive auth but
the token is invalid.'

def _Authenticate(self, http, needs_auth):

    if (needs_auth and ((not self.credentials) or self.credentials.invalid)):
        if self.oauth2_parameters.access_token:
            logger.debug('_Authenticate    skipping    auth    because    user    explicitly    supplied    an    access    token.')
            raise AuthPermanentFail('Access    token    is    invalid.')
        if self.oauth2_parameters.refresh_token:
            logger.debug('_Authenticate    skipping    auth    because    user    explicitly    supplied    a    refresh    token.')
            raise AuthPermanentFail('Refresh    token    is    invalid.')
        if self.oauth2_parameters.token_uri:
            logger.debug('_Authenticate    skipping    auth    because    user    explicitly    supplied    a    Token    URI,    for    example    for    service    account    authentication    with    Compute    Engine')
            raise AuthPermanentFail(('Token    URI    did    not    yield    a    valid    token:    ' + self.oauth_parameters.token_uri))
        logger.debug('_Authenticate    requesting    auth')
        flow = client.OAuth2WebServerFlow(client_id=self.oauth2_parameters.client_id, client_secret=self.oauth2_parameters.client_secret, scope=_ScopesToString(self.oauth2_parameters.scope), user_agent=self.user_agent)
        self.credentials = tools.run_flow(flow, self.storage, self.flags)
    if (self.credentials and (not self.credentials.invalid)):
        if ((not self.credentials.access_token_expired) or needs_auth):
            logger.debug('_Authenticate    configuring    auth;    needs_auth=%s', needs_auth)
            self.credentials.authorize(http)
            return
    logger.debug('_Authenticate    skipped    auth;    needs_auth=%s', needs_auth)
