0
votes

Im having what seems to be a very common error when working on the Spotify API adding tracks to a users playlist. In a previous fetch method I have obtained the users playlistId and am now trying to post tracks onto that playlist with that playlistId. I have followed the documentation but am obviously missing something, here is code:

    `        //userUd, playlistId, currentUserAccessToken, trackURIs are all defined 

fetch(`https://api.spotify.com/v1/users/${userId}/playlists/${playlistId}/tracks`, {
            headers: {
              'Authorization': 'Bearer ' + currentUserAccessToken
            },
            contentType: 'application/json',
            method: 'POST',
            body: JSON.stringify({
              "uris": `[${trackURIs}]`
            })
          }).then(success => {
            return success;
          }).catch(err => {
            console.log('here is your error', err);
          })`

I did the GET request to authorize user - included scope for creating public playlist, in different code block which is working, here's that:

`let scopes = 'playlist-modify-public';

window.location.replace(https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token);`

Thanks a ton!

1
trackURIs is correctly defined as a comma separated list of spotify URIs? - Anthony
yeah its an array of strings - the strings being the selected tracks uri - maxccpage
example would like like this {"uris":"[spotify:track:4omURIn6jRJ77sHxzSxor5,spotify:track:0mm5kY0FbqycYqXUvUBUn0]"} - maxccpage

1 Answers

0
votes

The JSON array needs quotes around each element in it. So each track should be quoted, like so:

{"uris": ["spotify:track:4iV5W9uYEdYUVa79Axb7Rh", "spotify:track:1301WleyT98MSxVHPZCA6M"]}

I think you're supplying an object names uris with an invalid array.

And, note you can pass the playlist items as query params too (instead of in the body), without using json. https://developer.spotify.com/web-api/add-tracks-to-playlist/