jwilsson / spotify-web-api-php

A PHP wrapper for Spotify's Web API.
MIT License
858 stars 157 forks source link

SpotifyWebAPIException: Missing or bad version tag #271

Open kasperkamperman opened 4 months ago

kasperkamperman commented 4 months ago

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?

jwilsson commented 4 months ago

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'
);
kasperkamperman commented 4 months ago

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

kasperkamperman commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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);
jwilsson commented 3 months ago

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.

vorce commented 3 months ago

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.

JMPerez commented 2 months ago

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.