jwilsson / spotify-web-api-php

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

Unknown Error On Callback #233

Closed amanuelanteneh closed 3 years ago

amanuelanteneh commented 3 years ago

I deployed an app using this library as a dependency to heroku and get this strange error message when trying to authenticate other users:

SpotifyWebAPI\SpotifyWebAPIException: An unknown error occurred. in /app/vendor/jwilsson/spotify-web-api-php/src/Request.php:59 Stack trace: #0 /app/vendor/jwilsson/spotify-web-api-php/src/Request.php(239): SpotifyWebAPI\Request->handleResponseError() #1 /app/vendor/jwilsson/spotify-web-api-php/src/Request.php(129): SpotifyWebAPI\Request->send() #2 /app/vendor/jwilsson/spotify-web-api-php/src/SpotifyWebAPI.php(124): SpotifyWebAPI\Request->api() #3 /app/vendor/jwilsson/spotify-web-api-php/src/SpotifyWebAPI.php(1556): SpotifyWebAPI\SpotifyWebAPI->sendRequest() #4 /app/callback.php(32): SpotifyWebAPI\SpotifyWebAPI->me() #5 {main}

The error seems to occur when I call the me() function after setting the access token. My auth.php is:

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    'REMOVED',
    'REMOVED',
    'https://app.com/callback.php'
);

$options = [
    'scope' => [
        'playlist-read-private',
        'user-read-private',
        'user-top-read',
        'user-read-recently-played',
        'playlist-modify-public',
    ]
];

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

die();

And my callback.php is:

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    'REMOVED',
    'REMOVED',
    'https://app.com/callback.php'
);

// Request a access token using the code from Spotify

$session->requestAccessToken($_GET['code']);

$accessToken = $session->getAccessToken();
$refreshToken = $session->getRefreshToken();

setcookie('accessToken', $accessToken, time() + (86400 * 30));
setcookie('refreshToken', $refreshToken, time() + (86400 * 30));

try {
$api = new SpotifyWebAPI\SpotifyWebAPI();

$api->setAccessToken($_COOKIE['accessToken']);

setcookie("user", $api->me()->display_name, time() + (86400 * 10));

header('Location: https://spotify-stats-php.herokuapp.com/index.php');
die();
}
catch (SpotifyWebAPI\SpotifyWebAPIException $e) {
    echo $e;
}

I'm not sure what could be causing this as I followed the documentation on the authorization almost exactly. What's odd is that I can log in just fine but other users can't seem to and get this error.

jwilsson commented 3 years ago

Hey! Have you created your app with Spotify recently? I think this might be related to the changes they recently made around app modes. I created a new app and a new Spotify account and got the same error. Unfortunately, Spotify doesn't seem to give any better errors back so it's not possible to show something other than the general "An unknown error occurred".

I think what you need to do is adding new users in your Developer dashboard or make a request for Extended Quota Mode (it's all outlined in their blog post).

Hope this solves the issue!

amanuelanteneh commented 3 years ago

That was the issue! For some reason my developer portal never updated with the new information. Thank you!