orcasgit / python-nokia

Library for the Nokia Health API
Other
57 stars 23 forks source link

Add refresh callback to CLI #31

Open brad opened 5 years ago

brad commented 5 years ago

Setup a refresh callback for the CLI, will fix #30

ethanopp commented 5 years ago

@brad Any update on this? Still confused how refresh and save tokens, to then use those saved tokens for the next api request.

brad commented 5 years ago

Sorry @ethanopp at this point this is not a needed feature for the product owner so it will not happen unless a community member contributes the code

ethanopp commented 5 years ago

Didn’t someone already do this?

https://github.com/orcasgit/python-nokia/pull/33

brad commented 5 years ago

@ethanopp The method in that PR manually refreshes tokens and doesn't save them for later (just updates the tokens for use in the current session)

A refresh callback is a method that takes a new token (automatically generated by oauthlib) as input and persists it somewhere (in this case, it should be a config file). Here is an example, but in that case the token is saved to the DB. Then when you create a NokiaApi you simply pass that method to the constructor and the rest is magic

ethanopp commented 5 years ago

@brad forgive me if I'm missing something here (a little new to developing with oauth 2), but what is the purpose of saving the creds then if you're never using them to make a request?

My understanding with the other apps I've used oauth2 on was that you save the creds, and then in the application do something like the following, so you're only gettting new tokens once the ones you saved have expired:

# Main code here where you pull current tokens for specific user from db and give to client()...then:

if time.time() > client.token_expires_at:
    refresh_response = client.refresh_access_token(client_id=1234, client_secret='asdf1234',
        refresh_token=client.refresh_token)

    # Code here to write new tokens to db for that user

    # Update current session with the new tokens
    client.access_token = refresh_response['access_token']
    client.refresh_token = refresh_response['refresh_token']
ethanopp commented 5 years ago

Dug through the code a little bit and think I found what I was looking for...

client = NokiaApi(creds, refresh_cb=save_withings_token)

Where save_withings_token is my method to save creds to db