spotipy/spotipy/util.py

94 lines
3.0 KiB
Python
Raw Normal View History

# shows a user's playlists (need to be authenticated via oauth)
from __future__ import print_function
import os
2015-06-05 09:07:28 +00:00
from . import oauth2
2014-08-22 13:23:06 +00:00
import spotipy
2014-08-22 13:23:06 +00:00
def prompt_for_user_token(username, scope=None, client_id = None,
client_secret = None, redirect_uri = None, cache_path = None):
''' prompts the user to login if necessary and returns
the user token suitable for use with the spotipy.Spotify
constructor
2014-08-22 13:23:06 +00:00
Parameters:
- username - the Spotify username
- scope - the desired scope of the request
- client_id - the client id of your app
- client_secret - the client secret of your app
- redirect_uri - the redirect URI of your app
- cache_path - path to location to save tokens
2014-08-22 13:23:06 +00:00
'''
2014-08-22 13:23:06 +00:00
if not client_id:
2014-08-22 15:00:29 +00:00
client_id = os.getenv('SPOTIPY_CLIENT_ID')
2014-08-22 13:23:06 +00:00
if not client_secret:
2014-08-22 15:00:29 +00:00
client_secret = os.getenv('SPOTIPY_CLIENT_SECRET')
2014-08-22 13:23:06 +00:00
if not redirect_uri:
2014-08-22 15:00:29 +00:00
redirect_uri = os.getenv('SPOTIPY_REDIRECT_URI')
2014-08-22 13:23:06 +00:00
if not client_id:
2015-06-05 09:07:28 +00:00
print('''
You need to set your Spotify API credentials. You can do this by
setting environment variables like so:
2014-08-22 15:00:29 +00:00
export SPOTIPY_CLIENT_ID='your-spotify-client-id'
export SPOTIPY_CLIENT_SECRET='your-spotify-client-secret'
export SPOTIPY_REDIRECT_URI='your-app-redirect-url'
2014-08-20 18:04:29 +00:00
Get your credentials at
https://developer.spotify.com/my-applications
2015-06-05 09:07:28 +00:00
''')
2014-08-22 13:23:06 +00:00
raise spotipy.SpotifyException(550, -1, 'no credentials set')
cache_path = cache_path or ".cache-" + username
sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri,
scope=scope, cache_path=cache_path)
# 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:
2015-06-05 09:07:28 +00:00
print('''
2014-08-20 18:04:29 +00:00
User authentication requires interaction with your
web browser. Once you enter your credentials and
give authorization, you will be redirected to
a url. Paste that url you were directed to to
complete the authorization.
2015-06-05 09:07:28 +00:00
''')
auth_url = sp_oauth.get_authorize_url()
try:
import webbrowser
webbrowser.open(auth_url)
print("Opened %s in your browser" % auth_url)
except:
2015-06-05 09:07:28 +00:00
print("Please navigate here: %s" % auth_url)
2014-08-20 18:04:29 +00:00
2015-06-05 09:07:28 +00:00
print()
print()
try:
response = raw_input("Enter the URL you were redirected to: ")
except NameError:
response = input("Enter the URL you were redirected to: ")
2015-06-05 09:07:28 +00:00
print()
print()
2014-08-22 13:23:06 +00:00
code = sp_oauth.parse_response_code(response)
token_info = sp_oauth.get_access_token(code)
# Auth'ed API request
if token_info:
return token_info['access_token']
else:
return None