diff options
Diffstat (limited to 'getspot.py')
| -rw-r--r-- | getspot.py | 32 |
1 files changed, 22 insertions, 10 deletions
@@ -68,30 +68,42 @@ BASE_URL = 'https://api.spotify.com/v1/' PLAYLIST_ID = '37i9dQZF1EMdXVibVNv347' # temp -def get_access_token(): +def get_auth_response(client_id=CLIENT_ID, client_secret=CLIENT_SECRET): """ - Get the temporary access token from the Spotify servers - (POST request) + Get the auth response from the spotify servers, with token and timeout info """ - auth_response = requests.post(AUTH_URL, { 'grant_type': 'client_credentials', - 'client_id': CLIENT_ID, - 'client_secret': CLIENT_SECRET, + 'client_id': client_id, + 'client_secret': client_secret, }) - # convert the response to JSON - auth_response_data = auth_response.json() + if not auth_response.ok: + raise SystemExit('Response from server not OK:\n %s' + % auth_response.text) + + return auth_response.json() + + +def get_access_token(): + """ + Get the temporary access token from the Spotify servers + (POST request) + """ + + auth_response_data = get_auth_response() # save the access token access_token = auth_response_data['access_token'] - return access_token + return access_token, auth_response_data def main(): """ main function """ - token = get_access_token() + token, auth_response = get_access_token() print(token) + print("Token expires in {expires} seconds.".format( + expires=auth_response['expires_in'])) if __name__ == '__main__': |
