Mel N.
Mel N.

Reputation: 15

Multiple errors when adding tracks to playlist using Spotipy

I am trying to add tracks to one of my Spotify playlists, and I'm pretty sure I have all of the code exactly right as described in the Spotipy documentation:

username = '*myusername*'
scope = 'playlist-modify-public'

playlist_id = '*myplaylistid*'
track_ids = *array of track ids*

token = util.prompt_for_user_token(username,
                           scope,
                           client_id='*myclientid*',
                           client_secret='*mysecretclientid*',
                           redirect_uri='http://localhost:8888/callback/')
spotify = spotipy.Spotify(auth=token)

results = spotify.user_playlist_add_tracks(username, playlist_id, track_ids)

However, these are the following errors two errors that I receive no matter what I try:

HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/users/*myusername*/playlists/*myplaylist*/tracks

During handling of the above exception, another exception occurred:

SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/users/*myusername*/playlists/*myplaylist*/tracks:
 Invalid track uri: spotify:track:*trackid*

It specifies Invalid track uri, however for each of the tracks in my list I have tested by searching the uri in Spotify and it is indeed valid.

Solutions I have tried to no avail:
1. Changing between ID and URIs for both playlists and track list
2. Authenticating using OAuth
3. Using different playlists and tracks
4. Using different redirect_uri

5 example track URIs for reference:

spotify:track:1rdHsnsGmleo6MRctkkFmm?si=7R0IKQ9xTgiwfLAJO7eFCw
spotify:track:70CMnzQ3FjMmUk5NPdQJBe?si=qL_WwgWVRTaSZ2oOBg2eCA
spotify:track:6bbx7nYlixYuElKMbYCzMm?si=Wu64S-obRaOOh3mFP3zWwA
spotify:track:6DZNQKNUskiWVSXs3cQPk3?si=SIW3hBU1SiWd_h1gpXwijg
spotify:track:2FMPIU8FdP9kCi5kUCSGnE?si=jtJOkQhsSF6GoD3otgtV3A

Would appreciate any help!! Thank you

Upvotes: 1

Views: 533

Answers (1)

Stéphane Bruckert
Stéphane Bruckert

Reputation: 22903

The track URIs should not contain ?si= but only what's before that. See https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids

You can fix your code to only keep the first part:

track_uris = [uri.split("?si=")[0] for uri in track_uris]

Upvotes: 1

Related Questions