I'm running into an error using the spotipy
package. It seems to say that the playlist ID or track ID isn't valid, but I can visit the link to both and they work. Unsure what I'm doing wrong. Any help is appreciated. I'm sure I'm missing something obvious.
import requests
import os
import spotipy
from spotipy.oauth2 import SpotifyOAuth
os.environ['SPOTIPY_CLIENT_ID'] = '$MY_CLIENT_ID'
os.environ['SPOTIPY_CLIENT_SECRET'] = '$MY_CLIENT_SECRET'
os.environ['SPOTIPY_REDIRECT_URI'] = 'http://localhost'
scope = "playlist-modify-public playlist-modify-private user-library-modify"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
sp.playlist_add_items('7AatTbqsTPhQ4bTRDqXaQH', '6nz35DNIzbtj5ztpDEcW1j')
This gives the following error:
sp.playlist_add_items('7AatTbqsTPhQ4bTRDqXaQH', '6nz35DNIzbtj5ztpDEcW1j')
HTTP Error for POST to https://api.spotify.com/v1/playlists/7AatTbqsTPhQ4bTRDqXaQH/tracks with Params: {'position': None} returned 400 due to Invalid base62 id
Traceback (most recent call last):
File ".../.venv/lib/python3.11/site-packages/spotipy/client.py", line 271, in _internal_call
response.raise_for_status()
File ".../.venv/lib/python3.11/site-packages/requests/models.py", line 1021, 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/playlists/7AatTbqsTPhQ4bTRDqXaQH/tracks
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../.venv/lib/python3.11/site-packages/spotipy/client.py", line 1085, in playlist_add_items
return self._post(
^^^^^^^^^^^
File ".../.venv/lib/python3.11/site-packages/spotipy/client.py", line 328, in _post
return self._internal_call("POST", url, payload, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../.venv/lib/python3.11/site-packages/spotipy/client.py", line 293, in _internal_call
raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/playlists/7AatTbqsTPhQ4bTRDqXaQH/tracks:
Invalid base62 id, reason: None
I've also tried running:
sp.playlist_add_items('spotify:playlist:7AatTbqsTPhQ4bTRDqXaQH', 'spotify:track:6nz35DNIzbtj5ztpDEcW1j')
And received URI errors. I also tried defining tracks as a list and passing it through and got the same error as above:
tracks = ['6nz35DNIzbtj5ztpDEcW1j']
sp.playlist_add_items('7AatTbqsTPhQ4bTRDqXaQH', tracks[0])
It may be due to missing API parameter or parameter value format. Check the API doc from here:
Example format and value is this:
sp.playlist_add_items(playlist_id = your_playlist_id, items =['spotify:episode:3nwfTNjbhDu8Cnp81TdmGO'], position=None)
So in this line of your code parameter names are missing:
sp.playlist_add_items('7AatTbqsTPhQ4bTRDqXaQH', '6nz35DNIzbtj5ztpDEcW1j')
Correct way is this:
sp.playlist_add_items(playlist_id = '7AatTbqsTPhQ4bTRDqXaQH', items =['6nz35DNIzbtj5ztpDEcW1j'], position=None)
Also check the discussion here.