Reputation: 319
I am using spotipy to retrieve some tracks from Spotify using python. I get a token expiration error thus, I want to refresh my token. But I don't understand how to get the refresh token from spotipy.
Is there another way to refresh the token or recreate one?
Thank you.
Upvotes: 5
Views: 6553
Reputation: 11
I saw the solution by mardiff which absolutely works, but I didn't like that it waits for an error to occur and then fixes it so I found a solution which doesn't require catching errors, using methods which spotipy already has implemented.
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import time
USERNAME = '...'
CLIENT_ID = '...'
CLIENT_SECRET = '...'
SCOPE = 'user-read-currently-playing'
def create_spotify():
auth_manager = SpotifyOAuth(
scope=SCOPE,
username=USERNAME,
redirect_uri='http://localhost:8080',
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET)
spotify = spotipy.Spotify(auth_manager=auth_manager)
return auth_manager, spotify
def refresh_spotify(auth_manager, spotify):
token_info = auth_manager.cache_handler.get_cached_token()
if auth_manager.is_token_expired(token_info):
auth_manager, spotify = create_spotify()
return auth_manager, spotify
if __name__ == '__main__':
auth_manager, spotify = create_spotify()
while True:
auth_manager, spotify = refresh_spotify(auth_manager, spotify)
playing = spotify.currently_playing()
if playing:
print(playing['item']['name'])
else:
print('Nothing is playing.')
time.sleep(30)
Using this method you check if the token is expired (or within 60 seconds of expiring) before each use of the spotify object. Creating new auth_manager and spotify objects as needed.
Upvotes: 0
Reputation: 1
Because this problem took me a while to figure out, I'm going to put my solution here. This works for running Spotipy on a server eternally (or at least has for the past 12 hours). You have to run it once locally to generate the .cache file, but once that has occurred, your server can then use that cache file to update it's access token and refresh token when needed.
import spotipy
scopes = 'ugc-image-upload user-read-playback-state user-modify-playback-state user-read-currently-playing ...'
sp = spotipy.Spotify(auth_manager=spotipy.SpotifyOAuth(scope=scopes))
while True:
try:
current_song = sp.currently_playing()
do something...
except spotipy.SpotifyOauthError as e:
sp = spotipy.Spotify(auth_manager=spotipy.SpotifyOAuth(scope=scopes))
Upvotes: 0
Reputation: 2444
The rough process that Spotipy takes with access tokens is:
prompt_for_user_token()
will handle you completing the OAuth flow in browser, after which it will save it to the cache.So it will be refreshed automatically if you ask Spotipy for your access token (e.g. with prompt_for_user_token()
or by setting up a SpotifyOAuth
object directly) and it has cached the access token / refresh token previously. Cache location should be .cache-<username>
in the working directory by default, so you can access the tokens manually there.
If you provide the Spotipy Spotify()
client with auth
param for authorization, it will not be able to refresh the access token automatically and I think it will expire after about an hour. You can provide it a client_credentials_manager
instead, which it will request the access token from. The only requirement of an implementation of the client_credentials_manager
object is that it provides a get_access_token()
method which takes no params and returns an access token.
I tried this out in a fork a while back, here's the modification to the SpotifyOAuth
object to allow it to act as a client_credentials_manager
and here's the equivalent of prompt_for_user_token()
that returns the SpotifyOAuth
object that you can pass to Spotipy Spotify()
client as a credentials manager param.
Upvotes: 4