mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-26 16:15:51 +03:00
[GH-ISSUE #263] Refreshing a token with authorization code flow #144
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#144
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?
Originally created by @nhend on GitHub (Mar 12, 2018).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/263
I have a long-running script using spotipy. After an hour (per the Spotify API), my access token expires. I am catching this successfully, but I don't know where to go from there in regards to actually refreshing the token. I am using the authorization code flow, not client credentials. Here's how I authorize:
`token = util.prompt_for_user_token(username,scope=scopes,client_id=client_id,client_secret=client_secret, redirect_uri=redirect_uri)
sp = spotipy.Spotify(auth=token)`
All refresh examples I've seen involve an
oauth2object (ex.oauth.refresh_access_token()), and the docs list only that function as a method of refreshing your token. It's my understanding that with authorization code flow, you don't need anoauthobject (because you authenticate withprompt_for_user_token(). If that's the case, how do I refresh my token?@onatilhan commented on GitHub (Mar 18, 2018):
+1
@nhend commented on GitHub (Mar 20, 2018):
Since I'm fairly certain refreshing a token without OAuth isn't possible, I'm linking my StackOverflow post where I detailed my solution and OAuth implementation, for Googler's sake
@kylegbakker commented on GitHub (Mar 20, 2018):
If you look at https://github.com/plamere/spotipy/blob/master/spotipy/util.py you can see it does all the oauth for you, however I am experiencing a similar issue where my tokens are not refreshing. I'm not sure if it is just my implementation or what. Available information is spotty at best.
@nhend commented on GitHub (Mar 21, 2018):
Yeah, but without actually having the OAuth object, you can't call any of the refresh functions. Perhaps refreshing should be built into the prompt_for_user_token() function, since that's where the OAuth object lives.
@kylegbakker commented on GitHub (Mar 21, 2018):
out of curiosity, what version of python are you running?
@nhend commented on GitHub (Mar 21, 2018):
3.6
@RaymondReed1208 commented on GitHub (Apr 29, 2018):
For what its worth, I've noticed that I am able to successfully refresh my spotify token when launching a python session in terminal, and running my code line by line... but if I try to do this in a python script, i get an invalid refresh token error
@devxpy commented on GitHub (Jun 2, 2018):
@nhend I haven't tried this myself, but these are my findings -
The token does get automatically refeshed when you call the
prompt_user_for_token()functionHere is the trace
sp_oauth.get_cached_token()inprompt_for_user_token()self.refresh_access_token(token_info['refresh_token'])inget_cached_token()However, you can see that
refresh_access_token()only gets called if it can find the cached token, and it's expired.So maybe try seeing if the token is cached in your local pc?
@josephgroton commented on GitHub (Nov 6, 2019):
Hi, I'm trying to authenticate, and I'm using the code snippet you posted above, but I keep getting the error: "NameError: name 'util' is not defined"
Would you happen to know why this might be happening?
@nhend commented on GitHub (Nov 6, 2019):
@josephgroton See this instead: https://stackoverflow.com/questions/49239516/spotipy-refreshing-a-token-with-authorization-code-flow
I used an OAuth object since the method I was originally posting about doesn't have a working refresh method.
@JattMones commented on GitHub (Jan 17, 2020):
I believe @devxpy is correct in that
refresh_access_token()only gets called by theprompt_for_user_token()function if it can find the cached token. However, I believe it should be able to find the refresh token in a file called '.cache-spotifyUsername', which should appear in your current working directory after you've authorized (ie. login with spotify username and password and entered the redirect url) your Spotify account with the app once. If you add the keywordcache_pathandpath\to\.cache-fileas the value inprompt_for_user_token(), it should be able to automatically refresh this token for you (this is because the refresh token never expires, it is only deactivated when requested).@JattMones commented on GitHub (Jan 17, 2020):
An example for Bob Smith's Spotify would be:
prompt_for_user_token(client_id='your_client_id', client_secret='your_client_secret', cache_path='.cache-BobSmith1234')I'm not exactly sure how the cache file is created since I haven't done that much testing. It may require you to do your initial authentication slightly differently, but still following the Spotify OAuth2.0 flow (that is if the file isn't created using the normal
prompt_for_user_token()function).@stephanebruckert commented on GitHub (Jan 26, 2020):
Here is a suggestion that replaces https://github.com/plamere/spotipy/issues/87#issuecomment-578539933, please let me know what you think:
Basically:
oauth_managerparameter, just like we have one for the other flowclient_credentials_managerhttps://github.com/plamere/spotipy/blob/master/examples/client_credentials_flow.pyutil.prompt_for_user_tokenwhich has always been a bit confusing. We would keep it but the documentation would not mention it anymoreoauth2.SpotifyOAuthis called only once and does everything thatutil.prompt_for_user_tokenused to do, that includes prompting for the token ifprompt=Trueand there is no cached tokenspotipy.Spotifywould be responsible for refreshing the token (it's already doing it for the client credentials flow)Or, keeping
util.prompt_for_user_token:@MaZderMind commented on GitHub (Jan 26, 2020):
Your first suggestion seems like a reasonable API design that would solve the problem neatly. 👍
@stephanebruckert commented on GitHub (Feb 10, 2020):
Done in https://github.com/plamere/spotipy/pull/435 thanks to @stefanondisponibile
Now there is no need for
util.prompt_for_user_tokenanymore. Instead you need to instantiate an oauth object. That will also take care of refreshing the token in the background so you shouldn't need to do it yourself.Example from https://github.com/plamere/spotipy/blob/master/examples/simple4.py:
@philipzimmermann commented on GitHub (Feb 12, 2020):
First of all thanks for spotipy. I am struggling with refreshing the token for a while now. Unfortunately, the example code does not seem to work for me. I tried:
But I'm getting a
TypeError: __init__() got an unexpected keyword argument 'auth_manager'@stefanondisponibile commented on GitHub (Feb 12, 2020):
Hey @phixxx5 , I guess that's because the changes have been merged to master but still haven't been released to pypi.
If you really can't wait or simply want to try the preview I think you could clone the repo and
pip install .it. But I'd suggest waiting for the next release, if possible 🙂@stephanebruckert commented on GitHub (Feb 12, 2020):
@phixxx5 creating a new release now
@stephanebruckert commented on GitHub (Feb 12, 2020):
Done, just do
pip install spotipy --upgradeI haven't updated the doc yet, but yes your code above should now work!