刷新URL时Spotify请求访问令牌不起作用

时间:2015-11-02 13:07:06

标签: php spotify

现在我正在研究spotify api,当我第一次点击url时它会给我所有播放列表但是当我再次刷新页面时它会给我异常错误,这是我的代码,

require '../../../vendor/autoload.php';
$session = new SpotifyWebAPI\Session('******', '*******', 'http://localhost:8080/spotify/vendor/jwilsson/spotify-web-api-php/demo.php'); 
$api = new SpotifyWebAPI\SpotifyWebAPI();

if (isset($_GET['code'])) {    
    $session->requestAccessToken($_GET['code']);
    $data = $api->setAccessToken($session->getAccessToken());

    $artistData = $api->me();
    $artistId = $artistData->id;
    $playlists = $api->getUserPlaylists($artistId, array(
        'limit' => 5
    ));

    foreach ($playlists->items as $playlist) {
        echo '<a href="' . $playlist->external_urls->spotify . '">' . $playlist->name . '</a> <br>';
    }
} else {
    $scopes = array(
        'scope' => array(
            'user-read-email',
            'user-library-modify',
        ),
    );

    header('Location: ' . $session->getAuthorizeUrl($scopes));
}

这是我的异常错误

Fatal error: Uncaught exception 'SpotifyWebAPI\SpotifyWebAPIException' with message 'Invalid authorization code' in D:\xampp\htdocs\spotify\vendor\jwilsson\spotify-web-api-php\src\Request.php:156 Stack trace: #0 D:\xampp\htdocs\spotify\vendor\jwilsson\spotify-web-api-php\src\Request.php(26): SpotifyWebAPI\Request->send('POST', 'https://account...', Array, Array) #1 D:\xampp\htdocs\spotify\vendor\jwilsson\spotify-web-api-php\src\Session.php(233): SpotifyWebAPI\Request->account('POST', '/api/token', Array) #2 D:\xampp\htdocs\spotify\vendor\jwilsson\spotify-web-api-php\demo.php(31): SpotifyWebAPI\Session->requestAccessToken('AQAaVZvpXUExKg2...') #3 {main} thrown in D:\xampp\htdocs\spotify\vendor\jwilsson\spotify-web-api-php\src\Request.php on line 156

有谁能告诉我为什么会导致错误?如果有人有spotify的经验

1 个答案:

答案 0 :(得分:1)

图书馆的创作者。

如果您尝试使用相同的代码尝试两次访问令牌,那么我能想到的唯一会产生错误消息的是。尝试再次访问PHP文件的URL,但删除所有?code=...部分。