-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix access token refreshing logic #874
Conversation
@@ -18,7 +18,7 @@ | |||
base_url = 'https://owner-api.teslamotors.com/api/1/vehicles' | |||
SETTINGS = { | |||
'DEBUG': False, | |||
'REFRESH_TOKEN': False, | |||
'refresh_token': False, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
most of the places use lower case, so update it to lowercase.
@@ -62,8 +62,8 @@ def _execute_request(url=None, method=None, data=None, require_vehicle_online=Tr | |||
|
|||
# Tesla REST Service sometimes misbehaves... this seems to be caused by an invalid/expired auth token | |||
# TODO: Remove auth token and retry? | |||
if result['response'] is None: | |||
_error("Fatal Error: Tesla REST Service returned an invalid response") | |||
if result.get('response') is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result['response']
threw exception when response
is not in the result. eg: { 'error': 'error msg'}
. Log the result in the next line would be helpful for error reporting IMO.
return tesla_api_json['access_token'] | ||
|
||
tesla = teslapy.Tesla(SETTINGS['tesla_email'], None) | ||
if SETTINGS['REFRESH_TOKEN'] or 0 < tesla.expires_at < time.time(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SETTINGS['REFRESH_TOKEN']
was always falsy due to the casing issue.
tesla.refresh_token() | ||
tesla_api_json['access_token'] = tesla.token.get('access_token') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The access_token
value from teslapy was not synced back to the tesla_api.json file. The teslapy
uses cache.json
file to save its own tokens.
The end result is the token in tesla_api_json['access_token']
already expired, but the token in tesla.token.get('access_token')
is ok after refresh. It causes the 401 expired token error because the line below uses the token from tesla_api.json
file.
d8c39d6
to
383a294
Compare
383a294
to
6588259
Compare
Currently, the access token expires after a short while due to the
teslapy
andtesla_api.json
out of sync issue.Tested the following cases in my local:
tesla_api.json
andcache.json
to simulate initial installtesla_api.json
to expired access tokentesla_api.json
andcache.json
to expired access tokens + expired expires_at timestamp.