'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.Authenication
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
    @util.positional(1)
    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 = {}
        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
        (resp, content) = request_orig(uri, method, body, headers, redirections, connection_type)
        if (resp.status in [401, 403]):
            logger.info(('Refreshing    due    to    a    %s' % str(resp.status)))
            self._refresh(request_orig)
            self.apply(headers)
            return request_orig(uri, method, body, headers, redirections, connection_type)
        else:
            return (resp, content)
    http.request = new_request
    setattr(http.request, 'credentials', self)
    return http
