Skip to content

Commit

Permalink
Refresh token when outdated
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuro committed Dec 1, 2023
1 parent 5b763b9 commit 95eb4c4
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion custom_components/nissan_connect/NissanConnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, username, password):
self._user_id = None
self._username = username
self._password = password
self._bearer_token = None
self._bearer_token = "ueGf2K1FfEwblf_JwC43bsrpp-8" #None
self._settings = {
"EU": {
"client_id": "a-ncb-prod-android",
Expand Down Expand Up @@ -42,6 +42,13 @@ def get_vehicles(self):
"Authorization": f"Bearer {self._bearer_token}"
}
)

# if response is 401, try to refresh token and try again
if req.status_code == 401:
self._bearer_token = None
self._refresh_api_token()
return self.get_vehicles()

req.raise_for_status()
_LOGGER.debug(req.json())
return req.json()
Expand All @@ -56,7 +63,15 @@ def get_location(self, vin):
"Authorization": f"Bearer {self._bearer_token}"
}
)

# if response is 401, try to refresh token and try again
if req.status_code == 401:
self._bearer_token = None
self._refresh_api_token()
return self.get_location(vin)

req.raise_for_status()

_LOGGER.debug(req.json())
return req.json()

Expand All @@ -70,6 +85,13 @@ def get_cockpit(self, vin):
"Authorization": f"Bearer {self._bearer_token}"
}
)

# if response is 401, try to refresh token and try again
if req.status_code == 401:
self._bearer_token = None
self._refresh_api_token()
return self.get_cockpit(vin)

req.raise_for_status()
_LOGGER.debug(req.json())
return req.json()
Expand Down

0 comments on commit 95eb4c4

Please sign in to comment.