From 9c9ae86a23683775528f53562edee69bff9a8966 Mon Sep 17 00:00:00 2001 From: David Todd Date: Tue, 24 Apr 2018 16:20:08 -0500 Subject: [PATCH] obtain_token_localhost() use same arguments as prompt_for_user_token() --- spotipy/util.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/spotipy/util.py b/spotipy/util.py index ec729f2..13f7d47 100644 --- a/spotipy/util.py +++ b/spotipy/util.py @@ -150,9 +150,49 @@ def start_local_http_server(port, handler=RequestHandler): return server -def obtain_token_localhost(username, client_id, client_secret, redirect_uri, cache_path=None, scope=None): +def obtain_token_localhost(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 + + 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 + + ''' + cache_path = cache_path or ".cache-" + username + if not client_id: + client_id = os.getenv('SPOTIPY_CLIENT_ID') + + if not client_secret: + client_secret = os.getenv('SPOTIPY_CLIENT_SECRET') + + if not redirect_uri: + redirect_uri = os.getenv('SPOTIPY_REDIRECT_URI') + + if not client_id: + print(''' + You need to set your Spotify API credentials. You can do this by + setting environment variables like so: + + export SPOTIPY_CLIENT_ID='your-spotify-client-id' + export SPOTIPY_CLIENT_SECRET='your-spotify-client-secret' + export SPOTIPY_REDIRECT_URI='your-app-redirect-url' + + Get your credentials at + https://developer.spotify.com/my-applications + ''') + raise spotipy.SpotifyException(550, -1, 'no credentials set') + sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri, scope=scope, cache_path=cache_path) token_info = sp_oauth.get_cached_token()