Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpotifyWebAPIException: Missing or bad version tag #271

Open
kasperkamperman opened this issue Feb 29, 2024 · 8 comments
Open

SpotifyWebAPIException: Missing or bad version tag #271

kasperkamperman opened this issue Feb 29, 2024 · 8 comments

Comments

@kasperkamperman
Copy link

kasperkamperman commented Feb 29, 2024

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
Copy link
Owner

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
Copy link
Author

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

@kasperkamperman
Copy link
Author

kasperkamperman commented Mar 13, 2024

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
Copy link
Author

kasperkamperman commented Mar 13, 2024

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
Copy link
Author

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
Copy link
Owner

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
Copy link

vorce commented Mar 21, 2024

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
Copy link
Contributor

JMPerez commented May 2, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants