Python API: check if authentication is still valid

Hi everyone :slight_smile:

I wrote a python script using the official python wrapper for the asana API. Authentication and all the others things i planed to do are working very well, but when my token expires after 60 minutes I struggle to detect that. Is there a method to check if authentication is still valid?
client.session.authorized
Seems not to be updated when the 60 minutes expire.

Thanks a lot and have a nice day!
Jakob

Hey @Jakob_Developer!

Good question. It looks like our library uses requests-oauthlib, who’s docs are here: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#refreshing-tokens

I think the best solution is the third option, automatic refreshing. With our lib in it’s current state, you should be able to do it with something like this:

asana.Client(session.AsanaOAuth2Session(
  client_secret="super_secret", 
  token={'access_token': accessToken},
  auto_refresh_url=refresh_url,
  token_updater=token_saver
))

Warning: The above code is psudocode, I haven’t tested that these are the right params in the right place.

This looks like something we will add to the client library in the future, as having you deal with the refresh logic can be a pain.

In the mean time, if you’d rather just get up and running asap. You can use the try except logic of the first or second options in the link.