Reputation: 29
I'm not so strong on Oauth so need some help to implement an interface to my FreeAgent accounting system, they provide an Oauth 2 API.
I want to code in standard Python and it seems the best library is Rauth. My problem is refreshing tokens.
Is there any good sample on how to refresh expired tokens using python Rauth library? What's then the best practice to handle the expiration? I could try to use my token and in case of error due to expiration ask for a refresh. Or maybe I could keep track of the life of a token and in case my computation says it's expired then ask for another. And where is best to save all this token information: in a configuration file, in JSON object, into a database ... ?
Thanks for any help.
Greg
Upvotes: 2
Views: 2607
Reputation: 1759
Or maybe I could keep track of the life of a token and in case my computation says it's expired then ask for another.
This is what I would recommend. Persist the expiry of the current token somewhere and before you make a request, check to see if the token has expired. If it has, use the refresh token with the access token methods to retrieve a fresh access token. The refresh_token
method could look something like this:
def refresh_token():
if not expired():
return
# OAuth 2.0 example
data = {'client_id':client_id,
'client_secret': client_secret,
'grant_type': 'refresh_token',
'refresh_token': refresh_token}
return service.get_access_token(data=data)
Because the exact process can vary slightly from provider to provider, it isn't documented by rauth. Perhaps we should make a note of this general pattern in the docs, however.
Hope that helps!
Upvotes: 6