mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-27 00:25:54 +03:00
[PR #428] [MERGED] Automatic refresh of Authorization Code Flow Tokens in long-running Applications #891
Labels
No labels
api-bug
bug
dependencies
documentation
duplicate
enhancement
external-ide
headless-mode
implicit-grant-flow
invalid
missing-endpoint
pr-welcome
private-api
pull-request
question
spotipy3
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/spotipy#891
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/spotipy-dev/spotipy/pull/428
Author: @MaZderMind
Created: 1/21/2020
Status: ✅ Merged
Merged: 1/22/2020
Merged by: @stephanebruckert
Base:
master← Head:auto-refresh-token📝 Commits (5)
d0593edauto-refresh user token3d85699example for a long-running user-request appd53b8ccwrap long lines5c8867ecombine duplicate code into _refresh_token_if_expired methode7a94daadd changelog entry📊 Changes
5 files changed (+68 additions, -16 deletions)
View changed files
📝
CHANGELOG.md(+2 -0)➕
examples/long_running.py(+42 -0)📝
spotipy/client.py(+4 -2)📝
spotipy/oauth2.py(+17 -9)📝
spotipy/util.py(+3 -5)📄 Description
This PR implements automatic refreshing of Authorization Code Flow Tokens.
Currently an
access_tokenrequested via the Authorization Code Flow have a lifetime of 3600 seconds (1 hour) after which a newaccess_tokenneeds to be requested using the 'refresh_token`.The Logic in
util.prompt_for_user_tokenlooks for a matching set of tokens in thecache_pathand if none is found, it requests both anaccess_tokenand arefresh_tokenfrom Spotify and stores them in thecache_path. If one is found, theaccess_tokenis checked for validity and if it is expired, a new one is requested and updated in thecache_path.This process works fine for short running applications, because the Token is automatically refreshed, whenever
util.prompt_for_user_tokenis invoked. For long running applications (with a runtime greater then the remaining lifetime of theaccess_tokenupon startup) this poses a problem, because theaccess_tokenis never refreshed, onceutil.prompt_for_user_tokenhas returned.To refresh the token later, the initialized instance of
oauth2.SpotifyOAuthis required. This instance is created inutil.prompt_for_user_tokenbut destroyed when the method returns.This PR changes the return-value of
util.prompt_for_user_tokento return the initializedoauth2.SpotifyOAuthinstead of theaccess_token. This instance is accepted as theauth-argument to thespotipy.Spotifyconstructor, so code that was just passing the token-string don't have to be changed.Furthermore
oauth2.SpotifyOAuthis changed to also cache theaccess_tokenin-memory, so that accessing it via the oauth2-instance does not have to hit the filesystem.The method
Spotify._auth_headersis responsible for adding the Auth-Token to the HTTP Request-Headers. It is changed to actively request a validaccess_tokenfrom the oauth2 instance. Theoauth2.SpotifyOAuthclass is changed to check the validity of the Token before returning and, if required, requesting a new Token.An Example of a long-running process which before exposed the problem is added. It does not run for multiple hours, refreshing the Token when it has expired before issuing any other request.
This probably fixes #263 and fixes #87
It probably also closes #428 by obsoleting it
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.