'Authorize an httplib2.Http instance with these credentials.
The modified http.request method will add authentication headers to each
request and will refresh access_tokens when a 401 is received on a
request. In addition the http.request method has a credentials property,
http.request.credentials, which is the Credentials object that authorized
it.
Args:
http: An instance of ``httplib2.Http`` or something that acts
like it.
Returns:
A modified instance of http that was passed in.
Example::
h = httplib2.Http()
h = credentials.authorize(h)
You can\'t create a new OAuth subclass of httplib2.Authentication
because it never gets passed the absolute URI, which is needed for
signing. So instead we have to overload \'request\' with a closure
that adds in the Authorization header and then calls the original
version of \'request()\'.'

def authorize(self, http):

    request_orig = http.request
    def new_request(uri, method='GET', body=None, headers=None, redirections=httplib2.DEFAULT_MAX_REDIRECTS, connection_type=None):
        if (not self.access_token):
            logger.info('Attempting    refresh    to    obtain    initial    access_token')
            self._refresh(request_orig)
        if (headers is None):
            headers = {}
        else:
            headers = dict(headers)
        self.apply(headers)
        if (self.user_agent is not None):
            if ('user-agent' in headers):
                headers['user-agent'] = ((self.user_agent + '    ') + headers['user-agent'])
            else:
                headers['user-agent'] = self.user_agent
        body_stream_position = None
        if all((getattr(body, stream_prop, None) for stream_prop in ('read', 'seek', 'tell'))):
            body_stream_position = body.tell()
        (resp, content) = request_orig(uri, method, body, clean_headers(headers), redirections, connection_type)
        max_refresh_attempts = 2
        for refresh_attempt in range(max_refresh_attempts):
            if (resp.status not in REFRESH_STATUS_CODES):
                break
            logger.info('Refreshing    due    to    a    %s    (attempt    %s/%s)', resp.status, (refresh_attempt + 1), max_refresh_attempts)
            self._refresh(request_orig)
            self.apply(headers)
            if (body_stream_position is not None):
                body.seek(body_stream_position)
            (resp, content) = request_orig(uri, method, body, clean_headers(headers), redirections, connection_type)
        return (resp, content)
    http.request = new_request
    setattr(http.request, 'credentials', self)
    return http
