Skip to content
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

Merged
merged 1 commit into from
Sep 6, 2024

Conversation

LeonBitCraft
Copy link

@LeonBitCraft LeonBitCraft commented Sep 5, 2024

Currently, the access token expires after a short while due to the teslapy and tesla_api.json out of sync issue.

Tested the following cases in my local:

  • Deleted both tesla_api.json and cache.json to simulate initial install
  • Manually updated tesla_api.json to expired access token
  • Manally updated both tesla_api.json and cache.json to expired access tokens + expired expires_at timestamp.

@@ -18,7 +18,7 @@
base_url = 'https://owner-api.teslamotors.com/api/1/vehicles'
SETTINGS = {
'DEBUG': False,
'REFRESH_TOKEN': False,
'refresh_token': False,
Copy link
Author

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:
Copy link
Author

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():
Copy link
Author

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')
Copy link
Author

@LeonBitCraft LeonBitCraft Sep 5, 2024

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.

@marcone marcone merged commit 06f4804 into marcone:main-dev Sep 6, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants