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

前端 未结 10 1882
失恋的感觉
失恋的感觉 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:19

    If you already have a Credentials object then you can refresh it like so:

    if refresh:
        import google_auth_httplib2
        # credentials instanceof google.oauth2.credentials.Credentials
        credentials.refresh(google_auth_httplib2.Request(httplib2.Http()))
    

    I had created the Credentials object from an old token JSON file like so:

        credentials = google.oauth2.credentials.Credentials(
            token=token_json['access_token'],
            refresh_token=token_json['refresh_token'],
            id_token=token_json['id_token'],
            token_uri=token_json['token_uri'],
            client_id=token_json['client_id'],
            client_secret=token_json['client_secret'],
            scopes=token_json['scopes'])
    

    In this way I was able to adapt some old oauth2client code.

提交回复
热议问题