diff options
| author | Willem Renes <wrenes@gmail.com> | 2021-05-02 13:22:48 +0200 |
|---|---|---|
| committer | Willem Renes <wrenes@gmail.com> | 2021-05-02 13:22:48 +0200 |
| commit | 580a88b1355c2ebfe902dd9f4fe7b4515edc77ab (patch) | |
| tree | 6afb5898799095ca0f896ee401297d3bc6edfb8b | |
| parent | c48f0d1aff455a8cb1bbbee6576532ecb8032eed (diff) | |
| download | GetSpot-auth.tar.gz GetSpot-auth.zip | |
Exit if server response is not OK.auth
| -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__': |
