spotify-web-api-php icon indicating copy to clipboard operation
spotify-web-api-php copied to clipboard

SpotifyWebAPIException: Missing or bad version tag

Open kasperkamperman opened this issue 1 year ago • 12 comments

I suddenly get an error when I'm deleting tracks. I didn't change any code or updated anything. Which makes me assume something changes on the side of Spotify (couldn't find announcements though).

Fatal error:  Uncaught SpotifyWebAPI\SpotifyWebAPIException: Missing or bad version tag in <redacted>/vendor/jwilsson/spotify-web-api-php/src/Request.php:47
Stack trace:
#0 <redacted>/vendor/jwilsson/spotify-web-api-php/src/Request.php(243): SpotifyWebAPI\Request->handleResponseError()
#1 <redacted>/vendor/jwilsson/spotify-web-api-php/src/Request.php(131): SpotifyWebAPI\Request->send()
#2 <redacted>/vendor/jwilsson/spotify-web-api-php/src/SpotifyWebAPI.php(122): SpotifyWebAPI\Request->api()
#3 <redacted>/vendor/jwilsson/spotify-web-api-php/src/SpotifyWebAPI.php(571): SpotifyWebAPI\SpotifyWebAPI->sendRequest()
#4 <redacted>/cronfresh.php(357): SpotifyWebAPI\SpotifyWebAPI->deletePlaylistTracks()
#5 <redacted>/cronfresh.php(231): addTrackToDestination()
#6 {main}
  thrown in <redacted>/vendor/jwilsson/spotify-web-api-php/src/Request.php on line 47

I delete tracks based on the positions:

$positionsToRemove = [
    [positions] => [
            [0] => 0
            [1] => 1
            [2] => 2
            [3] => 3
            [4] => 4
        ]
]

$api->deletePlaylistTracks($destinationId, $positionsToRemove, $snapShotId);

I was checking the Spotify API documentation and this way of deleting is not documented (anymore): https://developer.spotify.com/documentation/web-api/reference/remove-tracks-playlist

I looked quickly and it seems you didn't build anything to convert positions to spotify uri's. So, that might indeed indicate that there was a silent Spotify API change that remove this function?

kasperkamperman avatar Feb 29 '24 13:02 kasperkamperman

Hey! That's strange. It does sound like passing positions was removed from the Spotify docs at some point. But this code (copied from the tests added 6 years ago works 😅):

$api->deletePlaylistTracks(
    '2uavw0Rfozgta0baWuK3dm',
    [
        'positions' => [
            0,
            1,
        ],
    ],
    'NywxOTU1MGZiMmZhN2VhMzViMjMwMzIxMjQyYmQyODBjYzFlYWYxOGY1'
);

jwilsson avatar Feb 29 '24 16:02 jwilsson

I noticed it works again somehow. Will try if it keeps like this and share my findings.

kasperkamperman avatar Feb 29 '24 17:02 kasperkamperman

It worked for a week, now again Spotify refuses to delete songs.

I get the error: Missing or bad version tag

I tried removing the snapShotID but that results in: Must specify snapshot_id when setting positions/n

My assumption is that the positions part is removed or not stable. It's a pity. Because now I have to give track id's, however that would remove probably duplicates too (which I don't want).

kasperkamperman avatar Mar 13 '24 09:03 kasperkamperman

It seems that I have the same issues calling a supported (at last api documentation) way. Like passing track id's. Same error: Missing or bad version tag.

It might be an issue with the snapshotID then, which I get from a previous call to addPlaylistTracks.

$removeTracks = [];

    foreach ($destinationListData['items'] as $index => $item) {
        $epoch = strtotime($item['added_at']);
        //echo "epoch: ".$epoch."\n";
        if($epoch > $limitEpoch) {
            // jump out directly since all the other tracks are newer
            // so no need to check
            break;
        }

        $removeTracks[] = ['uri' => $item['track']['uri'], 'positions' => [$index]];
    }

    if($debug) {
        echo "removeTracks: ";
        print_r($removeTracks);
    }

    if(count($removeTracks)>0) {
        $tracksToRemove = [
            'tracks' => $removeTracks
        ];

        try {
            $api->deletePlaylistTracks($destinationId, $tracksToRemove, $snapShotId);
        }
        catch (Exception $e) {
            //$lastResponse = $api->getRequest()->getLastResponse();
            //var_dump($lastResponse);
            echo $e->getMessage()."/n";  
        }
              
    }

kasperkamperman avatar Mar 13 '24 19:03 kasperkamperman

I'm testing in the API console and there it still seems to work.

{
    "positions":  [0,1],
    "snapshot_id": "MTIsMzQ5NWMwNmZlOGE1MGYyOTg4ZGZhNzlhYWU2NjFiYjk2MGJlMjNiMg=="
}

What I noticed. I get a snapshotId when I call addPlaylistTracks. This snapshotId is the one I use to pass to deletePlaylistTracks($destinationId, $positionsToRemove, $snapShotId).

I could get a recent snapshotId in the api console by calling:

{
    "tracks": []
}

This works in this PHP api too:

$id = $api->deletePlaylistTracks($destinationId, $tracksToRemove);
echo "snapshot id:".$id;

What I noticed it that the snapshotId I got back from addPlaylistTracks is really shorter then the one I got from deletePlaylistTracks. This happens in this PHP code as well as in the Spotify console.

AAn2GSGCVTwzpvrMt9xvB/Dma6/IZkkW
NjUyODI5LDEyYTg1NzRhOWFiNTJlODZkYmI2YzAxNTczM2JjODg3MzI1ZGI4NzQ=

So my current working workaround is:

$new_id = $api->deletePlaylistTracks($destinationId, ['tracks' => []]);
echo "snapshot id:".$new_id;
$api->deletePlaylistTracks($destinationId, ['positions' => [0]], $new_id);

kasperkamperman avatar Mar 13 '24 20:03 kasperkamperman

I saw you're active in the thread on Spotify's forums. That was going to be my suggestion, to reach out there. But like you're saying there, it's really worrisome that the APIs change without any communication.

jwilsson avatar Mar 14 '24 16:03 jwilsson

Also having this problem with my little app (which has been working fine for years until lately), it uses the web api in this way:

snapshot_id = add_playlist_track(...)
delete_playlist_track(someuri, snapshot_id) // Will trigger 400, "Missing or bad version tag"

Haven't tried your workaround yet @kasperkamperman, but will do that.

vorce avatar Mar 21 '24 11:03 vorce

Adding another issue that I'm finding here in case you are trying to remove tracks at a specific position. I confirm that Spotify ends up removing all appearances of the track in the playlist and the positions are ignored. This is bad when trying to remove duplicates, as there is no way to leave one instance of the track.

JMPerez avatar May 02 '24 20:05 JMPerez

@JMPerez I was reading your blog on this issue. You mentioned

Update May 8th: Yet another fix deployed to bring back the previous behaviour.

João Vitor, author of Skiley, reached out and told me that it was possible to remove tracks by position alone ﹣ not combined with the song id ﹣ even though it's not documented. I have tested it and it works like a charm, meaning that the we won't need to reset the added time for the duplicated song.

I actually was using this way of removing tracks. Just by using positions, but I found it was broken when I wrote this issue. Are you actually using this way now in your Dedup tool and is it working stable?

My way of removing positions:

// create an array like [0,2,3,4]
 $positionsToRemove = [
      'positions' => range(0, $tooRemoveCount-1)
];

// because the previous snapShotId was not valid (see issue posts above)
$snapShotId = $api->deletePlaylistTracks($destinationId, ['tracks' => []]);
// delete the tracks (in my case at the beginning)
$api->deletePlaylistTracks($destinationId, $positionsToRemove, $snapShotId);

kasperkamperman avatar Nov 02 '24 10:11 kasperkamperman

I tested it again, but it doesn't seem to work anymore. I get the error message:

No uris provided

kasperkamperman avatar Nov 23 '24 14:11 kasperkamperman

Okay, this is really strange. I just tried deleting both by URI and position and both worked 🤔

$api->deletePlaylistTracks('3CuRKFFC2wwSMBwhPl74OT', [
    'tracks' => [
        [
            'uri' => 'spotify:track:2tzs8aV8k9xnbAiaHDn4YP',
        ],
    ],
]);
$response = $api->deletePlaylistTracks('3CuRKFFC2wwSMBwhPl74OT', [
    'positions' => [0],
]);

It's a playlist I just created, don't know if that matters. The only scope I have granted is "playlist-modify-private".

jwilsson avatar Nov 28 '24 19:11 jwilsson

Thanks @jwilsson for testing. I did another test myself.

I seems that my workaround (see earlier comments in this topic) for the invalid snapshot id threw this error: $new_id = $api->deletePlaylistTracks($destinationId, ['tracks' => []]);

I can confirm that removal of positions works again now. Fingers crossed, because it's undocumented in the API.

kasperkamperman avatar Dec 01 '24 09:12 kasperkamperman