jwilsson/spotify-web-api-php

since 2 days access tokens give an error

Closed this issue · 3 comments

Hi,

Since 1-2 day my spotify service broke down (basyl.stream). I didn't touch anything, so I'm figuring out what could be the cause of this.
The reason that I post this issue, is to figure out if other people had a same issue suddenly (maybe something changed on the Spotify side).

The strange thing is that a service I run with the same Spotify account and credentials still keeps working. For that service I don't use the "new" refresh implementation, but a try-catch function (as once suggested, before this was implemented in the web-api): #237 (comment)

Below the error details (I don't know if there exist already a better way to print it, since some details seem to be cut a way).

Fatal error:  Uncaught SpotifyWebAPI\SpotifyWebAPIException: Invalid access token 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(244): SpotifyWebAPI\Request->handleResponseError('{\n  "error": {\n...', 401)
#1 /<redacted>/vendor/jwilsson/spotify-web-api-php/src/Request.php(132): SpotifyWebAPI\Request->send('GET', 'https://api.spo...', 'fields=name%2Ce...', 'HTTP/2 401 \r\nww...')
#2 /<redacted>/vendor/jwilsson/spotify-web-api-php/src/SpotifyWebAPI.php(124): SpotifyWebAPI\Request->api('GET', '/v1/playlists/6...', Array, Array)
#3 /<redacted>/vendor/jwilsson/spotify-web-api-php/src/SpotifyWebAPI.php(1275): SpotifyWebAPI\SpotifyWebAPI->sendRequest('GET', '/v1/playlists/6...', Array)
#4 /<redacted>/vendor/jwilsson/spotify-web-api-php/src/Request.php on line 47

I did some tests.
The client_id/secret/refreshtoken seem to work fine. I'm able to obtain a new accessToken. However, when I use it print_r($api->me());, it directly says its invalid.

I made a minimal example. Like mentioned with an old version of your API (I'm not sure which version, I think 3 or 4) I can get access correctly.

I have this example and the print_r output of the $api. I'm curious if you see something wrong.

$session = new SpotifyWebAPI\Session(CLIENT_ID,CLIENT_SECRET);

$session->setAccessToken($spotifyAuth['sp_access_token']);
$session->setRefreshToken($spotifyAuth['sp_refresh_token']);

$options = [   
    "return_assoc" => true,
    "auto_refresh" => true,
    "auto_retry" => true
];

$api = new SpotifyWebAPI\SpotifyWebAPI($options,$session);
print_r($api);

$options = ['fields' => 'name, collaborative, followers, public, snapshot_id, tracks.total']; 
$sp_playlist_details = $api->getPlaylist($destinationList, $options);

print_r($sp_playlist_details);

This gives the following output:

SpotifyWebAPI\SpotifyWebAPI Object
(
    [accessToken:protected] => 
    [lastResponse:protected] => Array
        (
        )

    [options:protected] => Array
        (
            [auto_refresh] => 1
            [auto_retry] => 1
            [return_assoc] => 1
        )

    [request:protected] => SpotifyWebAPI\Request Object
        (
            [lastResponse:protected] => Array
                (
                )

            [options:protected] => Array
                (
                    [curl_options] => Array
                        (
                        )

                    [return_assoc] => 
                )

        )

    [session:protected] => SpotifyWebAPI\Session Object
        (
            [accessToken:protected] => <redacted>
            [clientId:protected] => <redacted>
            [clientSecret:protected] => <redacted>
            [expirationTime:protected] => 0
            [redirectUri:protected] => 
            [refreshToken:protected] => <redacted>
            [scope:protected] => 
            [request:protected] => SpotifyWebAPI\Request Object
                (
                    [lastResponse:protected] => Array
                        (
                        )

                    [options:protected] => Array
                        (
                            [curl_options] => Array
                                (
                                )

                            [return_assoc] => 
                        )

                )

        )

)


Fatal error:  Uncaught SpotifyWebAPI\SpotifyWebAPIException: Invalid access token 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(244): SpotifyWebAPI\Request->handleResponseError('{\n  "error": {\n...', 401)
#1 /<redacted>/vendor/jwilsson/spotify-web-api-php/src/Request.php(132): SpotifyWebAPI\Request->send('GET', 'https://api.spo...', 'fields=name%2C+...', 'HTTP/2 401 \r\nww...')
#2 /<redacted>/vendor/jwilsson/spotify-web-api-php/src/SpotifyWebAPI.php(124): SpotifyWebAPI\Request->api('GET', '/v1/playlists/5...', Array, Array)
#3 /<redacted>/vendor/jwilsson/spotify-web-api-php/src/SpotifyWebAPI.php(1275): SpotifyWebAPI\SpotifyWebAPI->sendReque in /<redacted>/vendor/jwilsson/spotify-web-api-php/src/Request.php on line 47

I figured out the error, it seems that accessTokens have a higher character count since a few days (I count 287). My database field to store the token was set to a maximum of 255 characters. 
My old implementation would request a new one and continue. Your implementation probably handles this in a different way (although I would expect also that it would continue with the call when the new access token is received).