Fix: API requests fail with 401 Unauthorized after token expires - #40
Open
szeredniklaszlo wants to merge 1 commit into
Open
Fix: API requests fail with 401 Unauthorized after token expires#40szeredniklaszlo wants to merge 1 commit into
szeredniklaszlo wants to merge 1 commit into
Conversation
Introduce SafeCaller to handle API request retries on authorization errors. Integrate SafeCaller across all API endpoint segments for robust error handling.
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.
Title:
Fix: API requests fail with 401 Unauthorized after token expiresSummary
This pull request introduces an automatic token refresh mechanism to fix a critical authentication bug. Currently, when a Spotify access token expires, all subsequent API requests fail with a
401 Unauthorizederror, forcing the user to manually log out and log back in.This is especially noticeable when opening Spotify links from external applications (e.g.,
https://open.spotify.com/track/...) using the method mentioned below, as the app opens but fails to load the track data, showing a placeholder screen.The Problem
The plugin's API endpoints (for tracks, artists, etc.) did not handle the
401 Unauthorizederror. When the token expired, they would throw an exception instead of triggering the existingrefreshCredentials()function in theauthmodule.The Solution
The fix is architecturally centralized, ensuring all API calls are resilient to token expiry:
Centralized Error Handling (
SafeCallerutility): A new utility class,SafeCaller, has been created. It wraps every API call in a function that uses Hetu's.then(catchError:)pattern. If a401error is detected, it automatically callsauth.refreshCredentials()and retries the original request with the new, valid token.Dependency Injection: The main
plugin.htnow injects theauthmodule into every endpoint, making the authentication context available wherever needed.Refactored Endpoints: All API-calling functions have been updated to use the
SafeCaller, making the retry logic consistent across the entire plugin.This ensures a seamless user experience, as token refreshes happen automatically in the background without interrupting the user.
Context for External Link Handling
This fix is crucial for users who rely on opening Spotify links via third-party tools (like URLCheck on Android) that map
https://open.spotify.com/...URLs to Spotube's internal deep links (e.g.,spotube://spotify/track/TRACK_ID). Without this patch, this core functionality becomes unreliable as soon as the session token expires.This PR makes that workflow robust and reliable again.
This may be related to these:
KRTirtho/spotube#2818
#36
#27