mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-27 00:25:54 +03:00
[GH-ISSUE #83] "401 error: The access token expired" when using SpotifyClientCredentials #38
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#38
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 @rinze on GitHub (Mar 6, 2016).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/83
I am creating my Spotipy object like this, after reading this code snippet:
However, after a number of queries, I obtain the following error:
I was under the impression that
SpotifyClientCredentialswas used to update the access token accordingly. Right now, I have my queries inside atry - exceptblock and I refresh it manually (I basically recreate thespobject every time I get an exception). Am I using this correctly or is there any other way?@Biggerandreas commented on GitHub (Feb 12, 2018):
Hi, I am having the same problem as you and I was wondering how you got the try - except to work? Right now it just seems to not catch the error for me.
@ritiek commented on GitHub (Feb 12, 2018):
@Biggerandreas You can use something like this to catch exception raised when the token expires:
@Biggerandreas commented on GitHub (Feb 12, 2018):
@ritiek I have already tried that but it still gives me this error:
Thanks in advance.
@ritiek commented on GitHub (Feb 12, 2018):
@Biggerandreas That should not be happening, I've been using something similar without problems. Do you mind sharing relevant code from your script here? (removing any credentials of course)
@Biggerandreas commented on GitHub (Feb 12, 2018):
@ritiek Sorry for the mess of a code but here it is:
@ritiek commented on GitHub (Feb 12, 2018):
@Biggerandreas Actually, there are couple of things you need to fix with your code. I'll try to explain my best. :)
Main.refresh_token()unnecessarily at multiple places, which would slow down your script. We only need to call it when aspotipy.client.SpotifyExceptionis raised. You should remove calling it from any other place.spotipy.client.SpotifyExceptionis raised when the token expires, we don't need to check it again inMain.refresh_token(). So, we can reduceMain.refresh_token()to something below:You could then just call
main.refresh_token()in yourexceptblock which should refresh token.main.get_song()undertryblock otherwise we cannot catch any exception it raises (in our case, we want to catchspotipy.client.SpotifyException).strwhich is not possible. Consider:As a workaround, we can convert them to
datetime.datetimeobjects and then subtract them:That should clear most stuff. Let me know if anything is still not right. :)
@Biggerandreas commented on GitHub (Feb 12, 2018):
Thanks for the help, will try to see if it works!
@Biggerandreas commented on GitHub (Feb 12, 2018):
@ritiek Sadly it did not work, maybe I am just doing it wrong.
You said I should put
main.get_song()in my main loop with a try block, but how would I do that?Is there a way to shorten the timeout? So I don't have to wait an hour for the error?
Thanks in advance.
@ritiek commented on GitHub (Feb 12, 2018):
My bad, that wasn't clear enough. You have this code:
You should put
Main.get_song()undertryblock like this so we could catch exceptions:Yep, kind of. For testing purposes, you could add
raise spotipy.client.SpotifyError(401, 401, 'ouch!')undertryblock as well:Of course, this won't let rest of the code in
tryblock (afterraise ...) run, you could just check out if it is generating a new token correctly or not by adding a simple print inMain.refresh_token():If it gives you a new token every time then you're probably good to go.
I can't actually test your complete code as I don't have any LCD display for my Pi.
@Biggerandreas commented on GitHub (Feb 12, 2018):
@ritiek Thanks! Now it catches it.
@Biggerandreas commented on GitHub (Feb 12, 2018):
@ritiek Now it catches the error but It does not seem to refresh the token because it just keeps printing, should I do something else in the try block?
@rinze commented on GitHub (Feb 12, 2018):
@Biggerandreas I went back to the code I was writing when I reported this issue. In the end it seems that what I did was simply to authenticate at the beginning of each loop I was doing (which retrieved some songs, but not so many that the auth would expire). This:
github.com/rinze/spotify-moods@847cfcbcf1/retriever.py (L78)In any case, shouldn't spotipy handle this internally?
@ritiek commented on GitHub (Feb 12, 2018):
@Biggerandreas You don't need to feed your refreshed token to
Main.spot().Main.refresh_token()automatically take cares of that withself.sp = spotipy.Spotify(auth=new_token['access_token']).You could just reduce your
exceptblock to:When your token expires (after 1 hour), your code will raise an exception
spotipy.client.SpotifyExceptionwhich will causeMain.refresh_token()to execute which should refresh your instance ofspotipy.Spotify()with your newly refreshed token, so the script should keep working fine.@rinze Yep, it would nice to have an optional argument to
spotipy.Spotify(auto_refresh=True)which would automatically refresh token once it expires. I don't see any active development on spotipy, so can't say how long before anything like this happens.@Biggerandreas commented on GitHub (Feb 12, 2018):
@ritiek Now it works, thanks for the help.
@stephanebruckert commented on GitHub (Jan 12, 2020):
Thanks @ritiek. You've been very helpful and active on this repo over the years. I'm currently the only maintainer on the repo, would you be interested to join me? If yes I will tweet your name to @plamere and see if he's ok. I see no reason why not, because Paul initially invited 2 more people but they aren't as available as expected.
@ritiek commented on GitHub (Jan 12, 2020):
Sure, thanks! That would be very nice.
@stephanebruckert commented on GitHub (Jan 12, 2020):
Great, thank you! You should have received the invite from @plamere
The goal is to automate RTD + PyPi deployments from a single account, but if needed we can still request it later