Steven
Steven

Reputation: 41

Refresh Access Token Python-Fitbit (Orcasgit)

I can't figure out how to refresh my access token using this: https://github.com/orcasgit/python-fitbit

I've been able to get my Access Token, and Refresh Token. I've also been able to pull data from fitbit. But after my Access Token Expires I'm lost. I think I'm supposed to create a FitbitOauth2Client object, and use the refresh_token function to get a new token. Below is the closest I've gotten.

tokenfresh=fitbit.FitbitOauth2Client(client_id=ci,client_secret=consumer_secret,access_token=at,refresh_token=rt)

I've scoured all over for an answer so any help would be much appreciated.

Upvotes: 2

Views: 823

Answers (1)

Pratik Sayare
Pratik Sayare

Reputation: 96

The problem is not your code, FitBit provides a new refresh token when you use an older refresh token to generate an access token. You should keep track of this refresh token in order make you code work. eg.

def fitbit_data(credentials):
    client_id = os.environ.get("FITBIT_CLIENT_ID")
    client_secret = os.environ.get("FITBIT_CLIENT_SECRET")

    oauth = fitbit.FitbitOauth2Client(client_id=client_id,
                                      client_secret=client_secret,
                                      refresh_token=str(credentials.get('refresh_token')),
                                      access_token=str(credentials.get('access_token')))
    token = oauth.refresh_token()
    update_refresh_token(token)

    app_client = fitbit.Fitbit(client_id=client_id, client_secret=client_secret,
                           access_token=token.access_token, refresh_token=token.refresh_token)
    steps = app_client.time_series(
        resource='activities/steps',
        period='1d'
    )
    return steps

Upvotes: 1

Related Questions