Skip to content

Commit d210fc6

Browse files
Save token after HA refresh grant and refresh 3 min before expiration (#715)
1 parent d3d1c19 commit d210fc6

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

app/core/payment/payment_tool.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,19 @@ def get_access_token(self) -> str:
7777
self._refresh_token = tokens["refresh_token"]
7878
self._access_token_expiry = tokens["expires_at"]
7979
# If we have a token but it's expired, we need to refresh it
80+
# We refresh the token if it's expired or if it's about to expire in less than 3 minutes
8081
elif self._access_token_expiry is None or self._access_token_expiry < int(
81-
datetime.now(UTC).timestamp(),
82+
datetime.now(UTC).timestamp() - 3 * 60,
8283
):
8384
# Token is expired, we need to refresh it
8485
try:
8586
tokens = self._auth_client.refresh_token(
8687
refresh_token=self._refresh_token,
8788
)
89+
self._access_token = tokens["access_token"]
90+
self._refresh_token = tokens["refresh_token"]
91+
self._access_token_expiry = tokens["expires_at"]
92+
return self.get_access_token()
8893
except Exception:
8994
hyperion_error_logger.exception(
9095
"Payment: failed to refresh HelloAsso access token, getting a new one",

0 commit comments

Comments
 (0)