[GH-ISSUE #304] Refreshing tokens doesn't work #173

Closed
opened 2026-02-27 23:21:12 +03:00 by kerem · 1 comment
Owner

Originally created by @stavlocker on GitHub (Jun 26, 2018).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/304

Hello. I have no idea what's going on.
I've waited an hour for my token to expire, then enabled pdb. I hope this explains my problem:

spotipy.client.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/me/player/currently-playing:
 The access token expired
(Pdb) token = util.prompt_for_user_token(username, scope)
(Pdb) spotify = spotipy.Spotify(auth=token) # And setting it as the new spotify object that I use of course
(Pdb) c # Try it again, now that the token is refreshed
...
spotipy.client.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/me/player/currently-playing:
 The access token expired
# Odd, isn't it? Lets reverse engineer to see what's the problem. Running parts of code from util.prompt_for_user_token():
(Pdb) sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri, scope=scope, cache_path=".cache-myusername")
(Pdb) sp_oauth.get_cached_token()['expires_at']
1530005354
(Pdb) now = int(time.time())
(Pdb) now
1530002630
(Pdb) sp_oauth.get_cached_token() < now
False # Token isn't expired

Why does the program say my token hasn't expired (When I refresh it with no issues whatsoever) when even the token itself shows that I still have more time, but Spotify says my token expired and won't let me do any requests?

Originally created by @stavlocker on GitHub (Jun 26, 2018). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/304 Hello. I have no idea what's going on. I've waited an hour for my token to expire, then enabled `pdb`. I hope this explains my problem: ```cmd spotipy.client.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/me/player/currently-playing: The access token expired (Pdb) token = util.prompt_for_user_token(username, scope) (Pdb) spotify = spotipy.Spotify(auth=token) # And setting it as the new spotify object that I use of course (Pdb) c # Try it again, now that the token is refreshed ... spotipy.client.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/me/player/currently-playing: The access token expired # Odd, isn't it? Lets reverse engineer to see what's the problem. Running parts of code from util.prompt_for_user_token(): (Pdb) sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri, scope=scope, cache_path=".cache-myusername") (Pdb) sp_oauth.get_cached_token()['expires_at'] 1530005354 (Pdb) now = int(time.time()) (Pdb) now 1530002630 (Pdb) sp_oauth.get_cached_token() < now False # Token isn't expired ``` Why does the program say my token hasn't expired (When I refresh it with no issues whatsoever) when even the token itself shows that I still have more time, but Spotify says my token expired and won't let me do any requests?
kerem closed this issue 2026-02-27 23:21:12 +03:00
Author
Owner

@stephanebruckert commented on GitHub (Jan 13, 2020):

(Pdb) sp_oauth.get_cached_token() < now
False # Token isn't expired

What you get from sp_oauth.get_cached_token() is a dictionary which when compared with an integer will always be False.

Instead you want to get token_info['access_token'] and use it as your new token.

Let me know if that helps, otherwise feel free to reopen

<!-- gh-comment-id:573902232 --> @stephanebruckert commented on GitHub (Jan 13, 2020): ``` (Pdb) sp_oauth.get_cached_token() < now False # Token isn't expired ``` What you get from `sp_oauth.get_cached_token()` is a dictionary which when compared with an integer will always be `False`. Instead you want to get `token_info['access_token']` and use it as your new token. Let me know if that helps, otherwise feel free to reopen
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#173
No description provided.