Google API: getting Credentials from refresh token with oauth2client.client

前端 未结 10 1869
失恋的感觉
失恋的感觉 2020-11-29 05:06

I am using googles official oauth2client.client to access the google plus api. I have a refresh token (that does not expire) stored in a database, and need to recreate the

10条回答
  •  失恋的感觉
    2020-11-29 05:07

    I solved this quite easily (you certainly miss this documentation). This is a snippet of my code that tries to use Picasa API to get all of album from active user:

        http = httplib2.Http(ca_certs=os.environ['REQUESTS_CA_BUNDLE'])
        try:
            http = self.oauth.credentials.authorize(http)
            response, album_list = http.request(Picasa.PHOTOS_URL, 'GET')
            if response['status'] == '403':
                self.oauth.credentials.refresh(http)
                response, album_list = http.request(Picasa.PHOTOS_URL, 'GET')
            album_list = json.load(StringIO(album_list))
        except Exception as ex:
            Logger.debug('Picasa: error %s' % ex)
            return {}
    

    Use the refresh method coming from oauth2client.client.OAuth2Credentials. I think it's even okay to use if response['status'] != '200'. Got to check that!

提交回复
热议问题