Added support for token caching and refresh

This commit is contained in:
Paul Lamere
2014-05-20 08:30:48 -04:00
parent 802c4b4354
commit 6ed7860b9d
2 changed files with 74 additions and 20 deletions

View File

@@ -17,29 +17,37 @@ else:
# Create these via developer.spotify.com/my-applications
client_id = os.getenv('CLIENT_ID')
client_secret = os.getenv('CLIENT_SECRET')
redirect_uri = os.getenv('REDIRECT_URI')
client_id = os.getenv('CLIENT_ID', 'YOUR_CLIENT_ID')
client_secret = os.getenv('CLIENT_SECRET', 'YOUR_CLIENT_SECRET')
redirect_uri = os.getenv('REDIRECT_URI', 'YOUR_REDIRECT_URI')
print 'client_id', client_id
print 'client_secret', client_secret
print 'redirect_uri', redirect_uri
# Oauth flow
sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri)
auth_url = sp_oauth.get_authorize_url()
try:
subprocess.call(["open", auth_url])
print "Opening %s in your browser" % auth_url
except:
print "Please navigate here: %s" % auth_url
sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri, cache_path=username)
response = raw_input("The URL you were redirected to: ")
code = sp_oauth.parse_response_code(response)
access_token = sp_oauth.get_access_token(code)
# try to get a valid token for this user, from the cache,
# if not in the cache, the create a new (this will send
# the user to a web page where they can authorize this app)
token_info = sp_oauth.get_cached_token()
if not token_info:
auth_url = sp_oauth.get_authorize_url()
try:
subprocess.call(["open", auth_url])
print "Opening %s in your browser" % auth_url
except:
print "Please navigate here: %s" % auth_url
response = raw_input("The URL you were redirected to: ")
code = sp_oauth.parse_response_code(response)
token_info = sp_oauth.get_access_token(code)
# Auth'ed API request
sp = spotipy.Spotify(auth=token_info['access_token'])
sp = spotipy.Spotify(auth=access_token)
playlists = sp.user_playlists(username)
pprint.pprint(playlists)
for playlist in playlists['items']:
print playlist['name']