07-14-2022 02:52
07-14-2022 02:52
I faced this error when I was running a Python script. I successfully ran the script several times before facing the error, and after the error, the script always fails with following messages.
Traceback (most recent call last):
File "handler.py", line 338, in <module>
handle(data, client, secrets)
File "handler.py", line 256, in handle
get_store_daily_score(client, fitbit_client, start_time, end_time)
File "handler.py", line 157, in get_store_daily_score
user = fitbit_client.user_profile_get()
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/fitbit/api.py", line 285, in user_profile_get
return self.make_request(url)
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/fitbit/api.py", line 256, in make_request
response = self.client.make_request(*args, **kwargs)
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/fitbit/api.py", line 90, in make_request
response = self. _request(
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/fitbit/api.py", line 75, in _request
self.refresh_token()
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/fitbit/api.py", line 156, in refresh_token
token = self.session.refresh_token(
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/requests_oauthlib/oauth2_session.py", line 446, in refresh_token
self.token = self._client.parse_request_body_response(r.text, scope=self.scope)
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 448, in parse_request_body_response
self.token = parse_token_response(body, scope=scope)
File "/Users/sato**ahem**raceback (most recent call last):
File "handler.py", line 338, in <module>
handle(data, client, secrets)
File "handler.py", line 256, in handle
get_store_daily_score(client, fitbit_client, start_time, end_time)
File "handler.py", line 157, in get_store_daily_score
user = fitbit_client.user_profile_get()
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/fitbit/api.py", line 285, in user_profile_get
return self.make_request(url)
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/fitbit/api.py", line 256, in make_request
response = self.client.make_request(*args, **kwargs)
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/fitbit/api.py", line 90, in make_request
response = self. _request(
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/fitbit/api.py", line 75, in _request
self.refresh_token()
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/fitbit/api.py", line 156, in refresh_token
token = self.session.refresh_token(
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/requests_oauthlib/oauth2_session.py", line 446, in refresh_token
self.token = self._client.parse_request_body_response(r.text, scope=self.scope)
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 448, in parse_request_body_response
self.token = parse_token_response(body, scope=scope)
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 441, in parse_token_response
validate_token_parameters(params)
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 448, in validate_token_parameters
raise_from_error(params.get('error'), params)
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/errors.py", line 399, in raise_from_error
raise cls(**kwargs)
oauthlib.oauth2.rfc6749.errors.InvalidGrantError: (invalid_grant)/opt/anaconda3/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 441, in parse_token_response
validate_token_parameters(params)
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 448, in validate_token_parameters
raise_from_error(params.get('error'), params)
File "/Users/satoshi/opt/anaconda3/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/errors.py", line 399, in raise_from_error
raise cls(**kwargs)
oauthlib.oauth2.rfc6749.errors.InvalidGrantError: (invalid_grant)
I saw other posts about the same error message, but I'm not sure if it's the same one because I tried the specified version of oauthlib (2.1.0) and requests-oauthlib (1.1.0) as well as oauthlib (3.2.0) and requests-oauthlib (1.3.1) but didn't get the luck. Also, I was able to run the script several times before the error.
Any idea what caused this error? I use Python client 0.3.1.
07-15-2022 07:33
07-15-2022 07:33
Hi @bigt23 ,
There are several things the "invalid grant" error could be. It's likely due to an invalid refresh token. To confirm, I'll need to look in our logs. Would you please private message me your client ID so I can confirm.
Thanks!
Gordon
07-15-2022 08:57 - edited 07-15-2022 08:58
07-15-2022 08:57 - edited 07-15-2022 08:58
Hi @bigt23
Thank you for sending over your client ID. There are a couple of things here.
1. Invalid grant error: It looks like the code is correctly refreshing the user's toke. Periodically, a previously refreshed token is used which is causing the error. Make certain you discard any old refresh tokens. Refresh tokens can only be used once before they become invalid.
2. Your application is specifying version 1.2 of the Activity time series endpoint. We do not support this version. You need to change your code to version 1 of the activity time series endpoints. See https://dev.fitbit.com/build/reference/web-api/activity-timeseries/get-activity-timeseries-by-date/. At anytime, we could disable access to Activity Time Series v1.2 which would cause your application to fail.
Sleep endpoints are the only ones that support version 1.2.
07-18-2022 06:20
07-18-2022 06:20
> 1. Make certain you discard any old refresh tokens.
I'll check my code again and update to you.
> 2. Your application is specifying version 1.2 of the Activity time series endpoint.
I'm using fitbit Python SDK ver 0.3.1 and use the following method for Activity time series endpoint.
response = fitbit_client.time_series(resource='activities/' + resource_type, base_date=base_date, period='1d')
How can I change the endpoint?
07-19-2022 18:34
07-19-2022 18:34
Who ever wrote the Python SDK coded the wrong endpoints. You'll need to look through the code and change it. Would you please share the link to the code?