From 901b2031a24ca30e064b7aba11ee60a73cb40576 Mon Sep 17 00:00:00 2001 From: Yasser Sinjab Date: Wed, 27 May 2020 23:26:47 +0300 Subject: [PATCH 1/3] Refactor SpotifyOauthError class - add error and error description to SpotifyOauthError to reflect the data returned by web api when error happens - unit test to SpotifyClientCredentials.get_access_token is added for invalid client --- spotipy/oauth2.py | 19 ++++++++++++++++--- tests/unit/test_oauth.py | 7 +++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/spotipy/oauth2.py b/spotipy/oauth2.py index b5ec15f..410499c 100644 --- a/spotipy/oauth2.py +++ b/spotipy/oauth2.py @@ -29,7 +29,10 @@ logger = logging.getLogger(__name__) class SpotifyOauthError(Exception): - pass + def __init__(self, message, error=None, error_description=None, *args, **kwargs): + self.error = error + self.error_description = error_description + super().__init__(message, *args, **kwargs) def _make_authorization_headers(client_id, client_secret): @@ -165,7 +168,12 @@ class SpotifyClientCredentials(SpotifyAuthBase): timeout=self.requests_timeout, ) if response.status_code != 200: - raise SpotifyOauthError(response.reason) + error_payload = response.json() + raise SpotifyOauthError( + 'error: {0}, error_description: {1}'.format( + error_payload['error'], error_payload['error_description']), + error=error_payload['error'], + error_description=error_payload['error_description']) token_info = response.json() return token_info @@ -431,7 +439,12 @@ class SpotifyOAuth(SpotifyAuthBase): timeout=self.requests_timeout, ) if response.status_code != 200: - raise SpotifyOauthError(response.reason) + error_payload = response.json() + raise SpotifyOauthError( + 'error: {0}, error_description: {1}'.format( + error_payload['error'], error_payload['error_description']), + error=error_payload['error'], + error_description=error_payload['error_description']) token_info = response.json() token_info = self._add_custom_values_to_token_info(token_info) self._save_token_info(token_info) diff --git a/tests/unit/test_oauth.py b/tests/unit/test_oauth.py index 2f8f544..2f5952f 100644 --- a/tests/unit/test_oauth.py +++ b/tests/unit/test_oauth.py @@ -6,6 +6,7 @@ import unittest import six.moves.urllib.parse as urllibparse from spotipy import SpotifyOAuth +from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOauthError try: import unittest.mock as mock @@ -166,3 +167,9 @@ class TestSpotifyOAuth(unittest.TestCase): parsed_url = urllibparse.urlparse(url) parsed_qs = urllibparse.parse_qs(parsed_url.query) self.assertTrue(parsed_qs['show_dialog']) + + def test_spotify_client_credentials_get_access_token(self): + oauth = SpotifyClientCredentials(client_id='ID', client_secret='SECRET') + with self.assertRaises(SpotifyOauthError) as error: + oauth.get_access_token() + self.assertEqual(error.exception.error, 'invalid_client') From 7da89b3137acc08e042559c7465ddee1933f1c39 Mon Sep 17 00:00:00 2001 From: Yasser Sinjab Date: Wed, 27 May 2020 23:38:44 +0300 Subject: [PATCH 2/3] Fix class initialization - make super python 2 compatible for SpotifyOauthError --- spotipy/oauth2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spotipy/oauth2.py b/spotipy/oauth2.py index 410499c..fdcdca3 100644 --- a/spotipy/oauth2.py +++ b/spotipy/oauth2.py @@ -32,7 +32,7 @@ class SpotifyOauthError(Exception): def __init__(self, message, error=None, error_description=None, *args, **kwargs): self.error = error self.error_description = error_description - super().__init__(message, *args, **kwargs) + super(SpotifyOauthError, self).__init__(message, *args, **kwargs) def _make_authorization_headers(client_id, client_secret): From 99f636d6e7ac8fe8531797a0d9ca4a11cf612dea Mon Sep 17 00:00:00 2001 From: Yasser Sinjab Date: Thu, 28 May 2020 02:23:16 +0300 Subject: [PATCH 3/3] Add changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7babdef..8c4ac62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - ... +## [2.12.1] - 2020-05-28 + +### Changed + + - Added two new attributes: error and error_description to `SpotifyOauthError` exception class to show + authorization/authentication web api errors details. + ## [2.12.0] - 2020-04-26 ### Added