spotipy: recommendations() requests.exceptions.HTTPError
I have the following code:
import spotipy
from spotipy.oauth2 import SpotifyOAuth
username = 'XXX'
client_id = 'XXX'
client_secret = 'XXX'
redirect_uri = 'http://localhost:8888/callback/'
scope = 'user-top-read user-read-recently-played playlist-modify-public playlist-modify-private'
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret,
redirect_uri=redirect_uri, scope=scope, username=username))
print(sp.recommendations(
seed_tracks=['spotify:track:7cm50Lw03k6VvRauJtkyTj', 'spotify:track:3zwMVvkBe2qIKDObWgXw4N',
'spotify:track:0KRYCBwIpWYFNrXOmXbyUh', 'spotify:track:5BK0uqwY9DNfZ630STAEaq',
'spotify:track:7lPN2DXiMsVn7XUKtOW1CS', 'spotify:track:1ZycJRpLOazlLcoFGxGROO',
'spotify:track:0lx2cLdOt3piJbcaXIV74f', 'spotify:track:3UoULw70kMsiVXxW0L3A33',
'spotify:track:4pfrrhvplbJZAIsfosGWQP', 'spotify:track:385VOmGbguaPG57TgoW8Lh'],
seed_artists=['spotify:artist:06HL4z0CvFAxyc27GXpf02', 'spotify:artist:6qqNVTkY8uBg9cP3Jd7DAH',
'spotify:artist:4AK6F7OLvEQ5QYCBNiQWHq', 'spotify:artist:163tK9Wjr9P9DmM0AVK7lm',
'spotify:artist:7n2wHs1TKAczGzO7Dd2rGr', 'spotify:artist:66CXWjxzNUsdJxJ2JdwvnR',
'spotify:artist:6LuN9FCkKOj5PcnpouEgny', 'spotify:artist:4dpARuHxo51G3z768sgnrY',
'spotify:artist:4nDoRrQiYLoBzwC5BhVJzF', 'spotify:artist:6KImCVD70vtIoJWnq6nGn3']))
and I am getting the following errors:
HTTP Error for GET to https://api.spotify.com/v1/recommendations returned 400 due to invalid request
Traceback (most recent call last):
File "/Library/Python/3.8/site-packages/spotipy/client.py", line 245, in _internal_call
response.raise_for_status()
File "/Users/carolynzech/Library/Python/3.8/lib/python/site-packages/requests/models.py", line 943, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/recommendations?limit=20&seed_artists=06HL4z0CvFAxyc27GXpf02%2C6qqNVTkY8uBg9cP3Jd7DAH%2C4AK6F7OLvEQ5QYCBNiQWHq%2C163tK9Wjr9P9DmM0AVK7lm%2C7n2wHs1TKAczGzO7Dd2rGr%2C66CXWjxzNUsdJxJ2JdwvnR%2C6LuN9FCkKOj5PcnpouEgny%2C4dpARuHxo51G3z768sgnrY%2C4nDoRrQiYLoBzwC5BhVJzF%2C6KImCVD70vtIoJWnq6nGn3&seed_tracks=7cm50Lw03k6VvRauJtkyTj%2C3zwMVvkBe2qIKDObWgXw4N%2C0KRYCBwIpWYFNrXOmXbyUh%2C5BK0uqwY9DNfZ630STAEaq%2C7lPN2DXiMsVn7XUKtOW1CS%2C1ZycJRpLOazlLcoFGxGROO%2C0lx2cLdOt3piJbcaXIV74f%2C3UoULw70kMsiVXxW0L3A33%2C4pfrrhvplbJZAIsfosGWQP%2C385VOmGbguaPG57TgoW8Lh
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/carolynzech/playlist-maker/main.py", line 69, in <module>
main()
File "/Users/carolynzech/playlist-maker/main.py", line 55, in main
print(sp.recommendations(
File "/Library/Python/3.8/site-packages/spotipy/client.py", line 1597, in recommendations
return self._get("recommendations", **params)
File "/Library/Python/3.8/site-packages/spotipy/client.py", line 291, in _get
return self._internal_call("GET", url, payload, kwargs)
File "/Library/Python/3.8/site-packages/spotipy/client.py", line 261, in _internal_call
raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/recommendations?limit=20&seed_artists=06HL4z0CvFAxyc27GXpf02%2C6qqNVTkY8uBg9cP3Jd7DAH%2C4AK6F7OLvEQ5QYCBNiQWHq%2C163tK9Wjr9P9DmM0AVK7lm%2C7n2wHs1TKAczGzO7Dd2rGr%2C66CXWjxzNUsdJxJ2JdwvnR%2C6LuN9FCkKOj5PcnpouEgny%2C4dpARuHxo51G3z768sgnrY%2C4nDoRrQiYLoBzwC5BhVJzF%2C6KImCVD70vtIoJWnq6nGn3&seed_tracks=7cm50Lw03k6VvRauJtkyTj%2C3zwMVvkBe2qIKDObWgXw4N%2C0KRYCBwIpWYFNrXOmXbyUh%2C5BK0uqwY9DNfZ630STAEaq%2C7lPN2DXiMsVn7XUKtOW1CS%2C1ZycJRpLOazlLcoFGxGROO%2C0lx2cLdOt3piJbcaXIV74f%2C3UoULw70kMsiVXxW0L3A33%2C4pfrrhvplbJZAIsfosGWQP%2C385VOmGbguaPG57TgoW8Lh:
invalid request, reason: None
Exception ignored in: <function Spotify.__del__ at 0x103da0af0>
Traceback (most recent call last):
File "/Library/Python/3.8/site-packages/spotipy/client.py", line 188, in __del__
TypeError: isinstance() arg 2 must be a type or tuple of types
Exception ignored in: <function SpotifyAuthBase.__del__ at 0x103e884c0>
Traceback (most recent call last):
File "/Library/Python/3.8/site-packages/spotipy/oauth2.py", line 136, in __del__
TypeError: isinstance() arg 2 must be a type or tuple of types
Any help would be much appreciated!
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 17 (10 by maintainers)
Once again I tried to find out what causes this and by simplifying my code step by step, I found out that adding a
from flask import Flask/import flaskin the same file as spotipy causes this issue (even if Flask is never used in any way).So this minimal example breaks for me both during development and on the main server:
I am using flask 2.0.2 and have absolutely no clue why this would happen.
Why don’t you just catch the error as I did here? Using
if requests.Sessionis a terrible idea because who knows what the__bool__method will return. If you’re trying to check if it’s notNone, then do so explicitly:if requests.Session is not None.If
requests.Sessionhas somehow been assigned toNone(which is what causes this exception), thenself._sessionwon’t be properly closed regardless of whether you use your updated version of__del__or the current version.The only way to ensure that the session is properly closed in the
__del__method is to find the underlying reason thatrequests.Sessionis being assigned toNone.