I have a script that searches for tracks using the spotify API everyhour. I have noticed that the script throws HTTPError 401 on some search requests. The script requests for new access token in every run. The following is how the authorization function works:
def authorize():
scope = 'playlist-modify-public'
username = os.environ.get('SPOTIFY_USERNAME')
client_id = os.environ.get('SPOTIPY_CLIENT_ID')
client_secret = os.environ.get('SPOTIPY_CLIENT_SECRET')
redirect_uri = os.environ.get('SPOTIPY_REDIRECT_URI')
token = SpotifyOAuth(client_id, client_secret, redirect_uri, scope=scope, username=username)
return token
This is what the error message looks like:
HTTP Error for GET to https://api.spotify.com/v1/search with Params: {'q': 'GoGo Penguin Ascent', 'limit': 5, 'offset': 0, 'type': 'track', 'market': 'US'} returned 401 due to None
Traceback (most recent call last):
File "/home/user/.local/lib/python3.9/site-packages/spotipy/client.py", line 271, in _internal_call
response.raise_for_status()
File "/usr/lib/python3/dist-packages/requests/models.py", line 943, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.spotify.com/v1/search?q=GoGo+Penguin+Ascent&limit=5&offset=0&type=track&market=US
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/user/repos/Autoplaylist/autoplaylist.py", line 143, in <module>
find_and_add_songs(playlist_id)
File "/home/user/repos/Autoplaylist/autoplaylist.py", line 68, in find_and_add_songs
result = spotifyObject.search(q=song, limit=5, offset=0, type='track', market='US')
File "/home/user/.local/lib/python3.9/site-packages/spotipy/client.py", line 587, in search
return self._get(
File "/home/user/.local/lib/python3.9/site-packages/spotipy/client.py", line 323, in _get
return self._internal_call("GET", url, payload, kwargs)
File "/home/user/.local/lib/python3.9/site-packages/spotipy/client.py", line 293, in _internal_call
raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/search?q=GoGo+Penguin+Ascent&limit=5&offset=0&type=track&market=US:
None, reason: None
I have tried limiting the rate of API calls and rotating API keys. None of these have worked so far.
Any input would be appreciated. Thank you!!