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
"401 error: The access token expired" when using SpotifyClientCredentials #83
Comments
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. |
@Biggerandreas You can use something like this to catch exception raised when the token expires: try:
do_something()
except spotipy.client.SpotifyException:
# re-authenticate when token expires
token = ...
sp = spotipy.Spotify(auth=token)
do_something() |
@ritiek I have already tried that but it still gives me this error:
Thanks in advance. |
@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) |
@ritiek Sorry for the mess of a code but here it is:
|
@Biggerandreas Actually, there are couple of things you need to fix with your code. I'll try to explain my best. :)
def refresh_token(self):
cached_token = self.spo.get_cached_token()
refreshed_token = cached_token['refresh_token']
new_token = self.spo.refresh_access_token(refreshed_token)
# also we need to specifically pass `auth=new_token['access_token']`
self.sp = spotipy.Spotify(auth=new_token['access_token'])
return new_token You could then just call
import time
time_started = time.strftime("%Y-%m-%d %H:%M:%S")
current_time = time.strftime("%Y-%m-%d %H:%M:%S")
print(time_started - current_time)
As a workaround, we can convert them to from datetime import datetime
import time
fmt = "%Y-%m-d %H:%M:%S"
time_started = time.strftime(fmt)
current_time = time.strftime(fmt)
# also i think you meant `current_time - time_started` and not other way round
print(datetime.strptime(current_time, fmt) - datetime.strptime(time_started, fmt)) That should clear most stuff. Let me know if anything is still not right. :) |
Thanks for the help, will try to see if it works! |
@ritiek Sadly it did not work, maybe I am just doing it wrong. Is there a way to shorten the timeout? So I don't have to wait an hour for the error? Thanks in advance. |
My bad, that wasn't clear enough. You have this code: while True:
new_track = main.get_song()
time.sleep(0.1)
try:
if new_track != old_track:
print(main.finder)
... You should put while True:
time.sleep(0.1)
try:
new_track = main.get_song()
if new_track != old_track:
print(main.finder)
...
Yep, kind of. For testing purposes, you could add while True:
time.sleep(0.1)
try:
raise spotipy.client.SpotifyError(401, 401, 'ouch!')
new_track = main.get_song()
if new_track != old_track:
print(main.finder) Of course, this won't let rest of the code in def refresh_token(self):
cached_token = self.spo.get_cached_token()
refreshed_token = cached_token['refresh_token']
new_token = self.spo.refresh_access_token(refreshed_token)
print(new_token['access_token']) # <--
# also we need to specifically pass `auth=new_token['access_token']`
self.sp = spotipy.Spotify(auth=new_token['access_token'])
return new_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. |
@ritiek Thanks! Now it catches it. |
@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?
|
@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: https://github.com/rinze/spotify-moods/blob/847cfcbcf1b463e50705b772821bf48acbb309fd/retriever.py#L78 while n_playlists < total_playlists:
sp = spotipy.Spotify(client_credentials_manager = scc)
[...] In any case, shouldn't spotipy handle this internally? |
@Biggerandreas You don't need to feed your refreshed token to You could just reduce your except spotipy.client.SpotifyException:
main.refresh_token()
print("Got an exception ") When your token expires (after 1 hour), your code will raise an exception
@rinze Yep, it would nice to have an optional argument to |
@ritiek Now it works, thanks for the help. |
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. |
Sure, thanks! That would be very nice. |
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 |
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
SpotifyClientCredentials
was used to update the access token accordingly. Right now, I have my queries inside atry - except
block and I refresh it manually (I basically recreate thesp
object every time I get an exception). Am I using this correctly or is there any other way?The text was updated successfully, but these errors were encountered: