2

Okay so I have been trying to wrap my head around google python api client library. This is what I have:

  1. Oauth2Credentials class -> Provides the method refresh() which allows for re-issue of a new access_token. In the client this is the only publicly visible method to reissue a fresh token. Further if a http request is authorized using an instance of this class the refresh is handled implicitly. Also, this instance should ideally not be created directly, but through the flow object. https://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.client.OAuth2Credentials-class.html

  2. AccessTokenCredentials class -> Accepts only the access_token and can only be used to validate the http requests. Does not provide the refresh method. https://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.client.AccessTokenCredentials-class.html

How do I use a refresh token to get a new access_token when it Oauth2Credentials instance is typically not to be created? Should I be storing the credentials object when first generated? If so how?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Varun Jain
  • 1,901
  • 7
  • 33
  • 66

1 Answers1

3

yes, you should be storing the access token in your database somehow.

When you load it from the DB, here is how to actually refresh it before you use it:

    #when you load the credentials from the DB, make sure it is 
    #this type of object:
    credentials = google.oauth2.credentials.Credentials()
    #here is the object (with type) that the credential object needs to
    #do the refreshing
    request = google.auth.transport.requests.Request()
    #and now we actually refresh the token.
    credentials.refresh(request)
    #and now the credentials object should be usable.

If you're looking for more details, I've done a write up on the whole oauth2 process using the google libraries from start to finish here:

https://stackoverflow.com/a/48242762/1626536

user1626536
  • 793
  • 1
  • 6
  • 14