2

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

Greggy
  • 29
  • 2

1 Answers1

6

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!

maxcountryman
  • 1,562
  • 1
  • 24
  • 51
  • Thanks Max, I'm going to try your suggestion. If I'm not wrong you are Rauth author, thank you for the support. – Greggy May 09 '13 at 21:28
  • Yes, I am the author. Please let me know if you have further questions. – maxcountryman May 10 '13 at 15:31
  • No Max, it doesn't work. I do this: `service = rauth.OAuth2Service(...) data = {.as you suggested...} return service.get_access_token(data=data)` but I got this error: `KeyError: 'Decoder failed to handle access_token with data as returned by provider. A different decoder may be needed. Provider returned: {"access_token":"xxxxxxxxxxxxxx","token_type":"bearer","expires_in":604800}' – Greggy May 15 '13 at 18:17
  • The problem was the standard decoder used by service. If I use a custom decoder that parse a string to a dictionary then everything works: `def str2dict(x): import ast return ast.literal_eval(x) def refresh_token() ... f = str2dict return service.get_access_token(data=data, decoder=f)` So fixed now. – Greggy May 15 '13 at 20:03
  • Sorry for the code all compacted.. How insert new lines or code sections here in StackOverflow comments ? – Greggy May 15 '13 at 20:06
  • No problem: multiline comments aren't supported in these comment threads as far as I know. – maxcountryman May 15 '13 at 21:47