Recover from a 401 by refreshing the token and retrying (no re-login)#711
Open
magne4000 wants to merge 1 commit into
Open
Recover from a 401 by refreshing the token and retrying (no re-login)#711magne4000 wants to merge 1 commit into
magne4000 wants to merge 1 commit into
Conversation
…ogin Previously a 401 from the Helix API showed a hard "OAuth token is required" dialog and exited, even when a valid refresh token was still stored -- which happens routinely when the short-lived access token lapses (Kodi or the network only just came up after a reboot or an overnight gap). The proactive refresh in Twitch.__init__ doesn't cover a token that expires or is rejected mid-session. error_check now treats a 401 with a stored refresh token as recoverable: it forces a silent refresh (ensure_valid_token(force=True)) and, on success, raises TokenRefreshed so api_error_handler reloads the new token onto the instance and retries the request once -- no toast, no re-login. A one-shot guard prevents any refresh/retry loop; if the retry still 401s while a refresh token survives, it bails quietly (the next visit or the proactive service refresh recovers). The hard sign-in dialog is now reached only when no refresh token remains -- a genuinely dead session. The private-credential 401 path is unchanged. Also add an offline guard: while Kodi reports no internet (System.InternetState), ensure_valid_token skips the doomed refresh instead of hanging ~15s on a dead connection; the stored token is kept and refreshed once connectivity returns. A forced post-401 recovery bypasses the guard. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Makes a mid-session 401 from the Helix API self-heal via the stored refresh token instead of forcing a re-login, and skips a doomed refresh while offline.
Motivation
Today a 401 shows a hard "OAuth token is required" dialog and exits, even when a valid refresh token is still stored — which happens routinely when the short-lived access token lapses (Kodi or the network only just came up after a reboot or an overnight gap). The proactive refresh in
Twitch.__init__doesn't cover a token that expires or is rejected mid-session.Changes
api.py error_check(now an instance method): on a 401 with a stored refresh token, force a silent refresh (ensure_valid_token(force=True)) and, on success, raiseTokenRefreshed. A one-shot per-instance guard prevents any refresh/retry loop.error_handling.api_error_handler(was a passthrough): catchesTokenRefreshed, reloads the new token onto the instance, and retries the request once.System.InternetState),ensure_valid_tokenskips the refresh instead of hanging ~15s on a dead connection; the stored token is kept and refreshed once connectivity returns. A forced post-401 recovery bypasses the guard.TokenRefreshedexception andkodi.connection_available()helper.Backward compatibility
The private-credential 401 path is unchanged. No settings or stored-token format changes.
The only user-visible change is fewer needless re-login prompts. No new strings.
Testing