diff --git a/spotipy/oauth2.py b/spotipy/oauth2.py index 0ed247f..8fd91b3 100644 --- a/spotipy/oauth2.py +++ b/spotipy/oauth2.py @@ -21,6 +21,17 @@ class SpotifyOAuth(object): def __init__(self, client_id, client_secret, redirect_uri, state=None, scope=None, cache_path=None): + ''' + Creates a SpotifyOAuth object + + Parameters: + - client_id - the client id of your app + - client_secret - the client secret of your app + - redirect_uri - the redirect URI of your app + - state - security state + - scope - the desired scope of the request + - cache_path - path to location to save tokens + ''' self.client_id = client_id self.client_secret = client_secret self.redirect_uri = redirect_uri @@ -50,9 +61,14 @@ class SpotifyOAuth(object): def save_token_info(self, token_info): if self.cache_path: - f = open(self.cache_path, 'w') - f.write(json.dumps(token_info)) - f.close() + try: + f = open(self.cache_path, 'w') + f.write(json.dumps(token_info)) + f.close() + except IOError: + self._warn("couldn't write token cache to " + self.cache_path) + pass + def is_token_expired(self, token_info): now = int(time.time()) diff --git a/spotipy/util.py b/spotipy/util.py index 6e4b17c..e836a73 100644 --- a/spotipy/util.py +++ b/spotipy/util.py @@ -3,21 +3,35 @@ import os import subprocess -import sys import oauth2 +import spotipy -def prompt_for_user_token(username, scope=None): +def prompt_for_user_token(username, scope=None, client_id = None, + client_secret = None, redirect_uri = 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 + ''' - 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') + if not client_id: + client_id = os.getenv('CLIENT_ID') + if not client_secret: + client_secret = os.getenv('CLIENT_SECRET') - if client_id == 'YOUR_CLIENT_ID': + if not redirect_uri: + redirect_uri = os.getenv('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: @@ -29,10 +43,10 @@ def prompt_for_user_token(username, scope=None): Get your credentials at https://developer.spotify.com/my-applications ''' - sys.exit(1) + raise spotipy.SpotifyException(550, -1, 'no credentials set') sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri, - scope=scope, cache_path=username) + scope=scope, cache_path=".cache-" + username ) # try to get a valid token for this user, from the cache, # if not in the cache, the create a new (this will send @@ -60,6 +74,9 @@ def prompt_for_user_token(username, scope=None): print print response = raw_input("Enter the URL you were redirected to: ") + print + print + code = sp_oauth.parse_response_code(response) token_info = sp_oauth.get_access_token(code) # Auth'ed API request