New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refreshing a token with authorization code flow #263
Comments
+1 |
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 |
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. |
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. |
out of curiosity, what version of python are you running? |
3.6 |
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 |
@nhend I haven't tried this myself, but these are my findings - The token does get automatically refeshed when you call the Here is the trace
However, you can see that So maybe try seeing if the token is cached in your local pc? |
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" |
@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. |
I believe @devxpy is correct in that |
An example for Bob Smith's Spotify would be: 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 |
Here is a suggestion that replaces #87 (comment), please let me know what you think:
Basically:
Or, keeping
|
Your first suggestion seems like a reasonable API design that would solve the problem neatly. 👍 |
Done in #435 thanks to @stefanondisponibile Now there is no need for Example from https://github.com/plamere/spotipy/blob/master/examples/simple4.py:
|
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 |
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 |
@phixxx5 creating a new release now |
Done, just do I haven't updated the doc yet, but yes your code above should now work! |
I added too a new way to connect to the OAuth API spotipy-dev/spotipy#263
In line with the following issue being fixed, changed authorisation flow to oauth. spotipy-dev/spotipy#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
oauth2
object (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 anoauth
object (because you authenticate withprompt_for_user_token()
. If that's the case, how do I refresh my token?The text was updated successfully, but these errors were encountered: