2

I'm trying to remove a song from an authenticated Spotify user's playlist and for some reason, I get Error 400 Missing Tracks, even though I know the song is there and the playlist is correct

I am using Pizzly from Bearer to handle the API request

Any idea what could be causing this issue? This is the link to Spotify's API Documentation

This is an example output of JSON.stringify(body)

{"tracks":[{"uri":"spotify:track:2fTdRdN73RgIgcUZN33dvt"}]}

   async removeSongsFromPlaylist(context, payload) {
        var tracks = payload[0]
        var playlistID = payload[1] || context.getters.user.favorites_playlist.id
        var authID = context.getters.spotifyAuthID
        var endpoint = `/playlists/${playlistID}/tracks`

        var uris = tracks.map(track => { return {uri: track.uri}})
        var body = {"tracks": uris}
        console.log(`URIS:`)
        console.log(body);
        console.log(`playlist: ${playlistID}`);
        return pizzly.integration("spotify").auth(authID).delete(endpoint, {
            body: JSON.stringify(body),
            headers: { "Content-Type": "application/json" }
            })
            .then(response => response.json())
            .then(responseJSON => {
                console.log("song successfully deleted")
                console.log(responseJSON)
            })
            .catch((err) => console.log(err))
        

    },

Update: I did make some progress with a very hacky solution

I took Spotify's cURL example, converted it to fetch with this link and put that inside my function as so:

        fetch(`https://api.spotify.com/v1/playlists/${playlistID}/tracks`, {
            body: "{\"tracks\":[{\"uri\":\"" + tracks[0].uri + "\"}]}",
            headers: {
                Accept: "application/json",
                Authorization: "Bearer ~accessTokenHiddenForPrivacy~",
                "Content-Type": "application/json"
            },
            method: "DELETE"
        })     
        .then(response => response.json())

Fortunately it worked! The problem is that I tried copy and pasting the body from below into the code above and it still shows "Missing Tracks". I cannot keep this code below without another way of getting the user's access token- which is currently handled by pizzly and the auth id.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
degenPenguin
  • 725
  • 1
  • 8
  • 23
  • 1
    Might the error message mean that Spotify isn't receiving the `tracks` parameter in the expected way? Perhaps you could examine the output of `JSON.stringify(body)`, and share it with us you don't see a problem (and you agree it might help). – Noah Dec 22 '20 at 02:52
  • Did not think of this! Happy to - `{"tracks":[{"uri":"spotify:track:2fTdRdN73RgIgcUZN33dvt"}]} ` – degenPenguin Dec 22 '20 at 02:57
  • Looks well-formed to me! – Noah Dec 22 '20 at 03:03
  • Yes! And actually I made some progress from your comment, however - it will not suffice! But it did disprove that theory. Am updating the main post with info. – degenPenguin Dec 22 '20 at 03:04
  • Basically I produced a hacky solution but the issue is I use pizzly to store the user's access token, which is currently hard-coded. – degenPenguin Dec 22 '20 at 03:05
  • Huh, If Spotify wants all those escaped quotes, would invoking `stringify` twice do the trick? (Suggested in [this answer](https://stackoverflow.com/a/48577224/7711863).) – Noah Dec 22 '20 at 03:13
  • 1
    I gave it a shot to no avail. Given that I tried to copy and paste the `fetch` body into the `pizzly` body and it failed makes me think the offender is not in the body param. But i dont know where the issue would be otherwise – degenPenguin Dec 22 '20 at 03:19

0 Answers0