@@ -97,17 +97,20 @@ async def _get_oauth2_credentials(
97
97
linked_account .security_credentials
98
98
)
99
99
if _access_token_is_expired (oauth2_scheme_credentials ):
100
- # Instagram's access token only could be refreshed with a valid access token, so we need to re-authorize
100
+ # Instagram's access token only could be refreshed with a valid access token, so we need to re-authorize if invalid
101
101
if app .name == "INSTAGRAM" :
102
- logger .error (
103
- f"Access token expired, please re-authorize, linked_account_id={ linked_account .id } , "
104
- f"security_scheme={ linked_account .security_scheme } , app={ app .name } "
105
- )
106
- # NOTE: this error message could be used by the frontend to guide the user to re-authorize
107
- raise OAuth2Error (
108
- f"Access token expired. Please re-authorize at: "
109
- f"{ config .DEV_PORTAL_URL } /appconfigs/{ app .name } "
110
- )
102
+ # Since _access_token_is_expired returned True, expires_at is guaranteed to be not None
103
+ actual_expires_at = oauth2_scheme_credentials .expires_at + 86400 # type: ignore[operator]
104
+ if int (time .time ()) > actual_expires_at :
105
+ logger .error (
106
+ f"Access token expired, please re-authorize, linked_account_id={ linked_account .id } , "
107
+ f"security_scheme={ linked_account .security_scheme } , app={ app .name } "
108
+ )
109
+ # NOTE: this error message could be used by the frontend to guide the user to re-authorize
110
+ raise OAuth2Error (
111
+ f"Access token expired. Please re-authorize at: "
112
+ f"{ config .DEV_PORTAL_URL } /appconfigs/{ app .name } "
113
+ )
111
114
112
115
logger .warning (
113
116
f"Access token expired, trying to refresh linked_account_id={ linked_account .id } , "
@@ -116,6 +119,7 @@ async def _get_oauth2_credentials(
116
119
token_response = await _refresh_oauth2_access_token (
117
120
app .name , oauth2_scheme , oauth2_scheme_credentials
118
121
)
122
+
119
123
# TODO: refactor parsing to _refresh_oauth2_access_token
120
124
expires_at : int | None = None
121
125
if "expires_at" in token_response :
0 commit comments