[GH-ISSUE #189] HTTP Error 500 on requestAccessToken() #124

Closed
opened 2026-02-27 19:26:12 +03:00 by kerem · 6 comments
Owner

Originally created by @Keithtron on GitHub (May 3, 2020).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/189

Hi!

I've been going nuts trying to debug this error 500 I've been getting. The Spotify user authentication seems to go through ok, and when it redirects to the next PHP page, it errors on this: $session->requestAccessToken($_GET['code']);

I've gotten this working fine locally in XAMPP, but when I upload it to my AWS EC2 server (and change redirect URLs accordingly), it's not working.

I've stripped down that php page to only the following:

require_once 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    '.......',
    '.......',
    '.......');

// Request a access token using the code from Spotify
$session->requestAccessToken($_GET['code']);
//$accessToken = $session->getAccessToken();

phpinfo();

This executes fine if I comment out the requestAccessToken, but give error 500 when running that command.

Any ideas on this? Thanks in advance for any help you can provide!

Originally created by @Keithtron on GitHub (May 3, 2020). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/189 Hi! I've been going nuts trying to debug this error 500 I've been getting. The Spotify user authentication seems to go through ok, and when it redirects to the next PHP page, it errors on this: $session->requestAccessToken($_GET['code']); I've gotten this working fine locally in XAMPP, but when I upload it to my AWS EC2 server (and change redirect URLs accordingly), it's not working. I've stripped down that php page to only the following: ``` require_once 'vendor/autoload.php'; $session = new SpotifyWebAPI\Session( '.......', '.......', '.......'); // Request a access token using the code from Spotify $session->requestAccessToken($_GET['code']); //$accessToken = $session->getAccessToken(); phpinfo(); ``` This executes fine if I comment out the requestAccessToken, but give error 500 when running that command. Any ideas on this? Thanks in advance for any help you can provide!
kerem 2026-02-27 19:26:12 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@Keithtron commented on GitHub (May 3, 2020):

My localhost XAMPP is running php 7.3.11 with curl 7.64.0, and the EC2 server is running php 7.0.16 with curl 7.61.1. But I assume this shouldn't be an issue, since they're both version 7 with curl support.

<!-- gh-comment-id:623129266 --> @Keithtron commented on GitHub (May 3, 2020): My localhost XAMPP is running php 7.3.11 with curl 7.64.0, and the EC2 server is running php 7.0.16 with curl 7.61.1. But I assume this shouldn't be an issue, since they're both version 7 with curl support.
Author
Owner

@Keithtron commented on GitHub (May 3, 2020):

I've also tried your user data display example:

require_once 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(............);

$api = new SpotifyWebAPI\SpotifyWebAPI();

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

 //   print_r($api->me());
 phpinfo();
} else {
    $options = [
        'scope' => [
            'user-read-email',
        ],
    ];

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

Same issue here, with the error 500 on requestAccessToken, but when I swap in the phpinfo() instead, that runs fine.

<!-- gh-comment-id:623130448 --> @Keithtron commented on GitHub (May 3, 2020): I've also tried your user data display example: ``` require_once 'vendor/autoload.php'; $session = new SpotifyWebAPI\Session(............); $api = new SpotifyWebAPI\SpotifyWebAPI(); if (isset($_GET['code'])) { // $session->requestAccessToken($_GET['code']); // $api->setAccessToken($session->getAccessToken()); // print_r($api->me()); phpinfo(); } else { $options = [ 'scope' => [ 'user-read-email', ], ]; header('Location: ' . $session->getAuthorizeUrl($options)); die(); } ``` Same issue here, with the error 500 on requestAccessToken, but when I swap in the phpinfo() instead, that runs fine.
Author
Owner

@jwilsson commented on GitHub (May 3, 2020):

Hey!
That's really strange! Can you see the exact PHP error you're getting?

For example, by putting this at the top of the file:

error_reporting(-1); 
ini_set('display_errors', 'on');

Thanks!

<!-- gh-comment-id:623152239 --> @jwilsson commented on GitHub (May 3, 2020): Hey! That's really strange! Can you see the exact PHP error you're getting? For example, by putting this at the top of the file: ```php error_reporting(-1); ini_set('display_errors', 'on'); ``` Thanks!
Author
Owner

@Keithtron commented on GitHub (May 3, 2020):

Big thanks for the quick response!

Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIAuthException: Invalid redirect URI in /var/www/html/hybrid-blak/gate/vendor/jwilsson/spotify-web-api-php/src/Request.php:65 Stack trace: #0 /var/www/html/hybrid-blak/gate/vendor/jwilsson/spotify-web-api-php/src/Request.php(265): SpotifyWebAPI\Request->parseBody(Object(stdClass), 400) #1 /var/www/html/hybrid-blak/gate/vendor/jwilsson/spotify-web-api-php/src/Request.php(115): SpotifyWebAPI\Request->send('POST', 'https://account...', 'client_id=692b4...', Array) #2 /var/www/html/hybrid-blak/gate/vendor/jwilsson/spotify-web-api-php/src/Session.php(217): SpotifyWebAPI\Request->account('POST', '/api/token', Array, Array) #3 /var/www/html/hybrid-blak/gate/index.php(17): SpotifyWebAPI\Session->requestAccessToken('AQC7yhTEh-lr04V...') #4 {main} thrown in /var/www/html/hybrid-blak/gate/vendor/jwilsson/spotify-web-api-php/src/Request.php on line 65

While putting this thing together I was able to fix some other 'Invalid Redirect URI' errors, but I'm curious why this one would manifest as an Error 500 rather than displaying the error more directly, like it does with those extra error reporting settings. (Perhaps different error reporting settings on the real server vs my XAMPP localhost?)

The redirect URI in my call to '$session = new SpotifyWebAPI\Session(...)' seems to be valid and is included in the settings in the client dashboard.

<!-- gh-comment-id:623170089 --> @Keithtron commented on GitHub (May 3, 2020): Big thanks for the quick response! ` Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIAuthException: Invalid redirect URI in /var/www/html/hybrid-blak/gate/vendor/jwilsson/spotify-web-api-php/src/Request.php:65 Stack trace: #0 /var/www/html/hybrid-blak/gate/vendor/jwilsson/spotify-web-api-php/src/Request.php(265): SpotifyWebAPI\Request->parseBody(Object(stdClass), 400) #1 /var/www/html/hybrid-blak/gate/vendor/jwilsson/spotify-web-api-php/src/Request.php(115): SpotifyWebAPI\Request->send('POST', 'https://account...', 'client_id=692b4...', Array) #2 /var/www/html/hybrid-blak/gate/vendor/jwilsson/spotify-web-api-php/src/Session.php(217): SpotifyWebAPI\Request->account('POST', '/api/token', Array, Array) #3 /var/www/html/hybrid-blak/gate/index.php(17): SpotifyWebAPI\Session->requestAccessToken('AQC7yhTEh-lr04V...') #4 {main} thrown in /var/www/html/hybrid-blak/gate/vendor/jwilsson/spotify-web-api-php/src/Request.php on line 65 ` While putting this thing together I was able to fix some other 'Invalid Redirect URI' errors, but I'm curious why this one would manifest as an Error 500 rather than displaying the error more directly, like it does with those extra error reporting settings. (Perhaps different error reporting settings on the real server vs my XAMPP localhost?) The redirect URI in my call to '$session = new SpotifyWebAPI\Session(...)' seems to be valid and is included in the settings in the client dashboard.
Author
Owner

@Keithtron commented on GitHub (May 3, 2020):

Looks like I got it working!
I had tried several different redirect URIs that matched entries in my client dashboard, all ending with "/", which had sounded like a recommendation from some other posts I had seen. But it finally worked when I set the redirect URI to match the currently running script.

Your suggestion to enable proper error reporting was a huge help, I couldn't have tracked this down otherwise. Thanks!

<!-- gh-comment-id:623174074 --> @Keithtron commented on GitHub (May 3, 2020): Looks like I got it working! I had tried several different redirect URIs that matched entries in my client dashboard, all ending with "/", which had sounded like a recommendation from some other posts I had seen. But it finally worked when I set the redirect URI to match the currently running script. Your suggestion to enable proper error reporting was a huge help, I couldn't have tracked this down otherwise. Thanks!
Author
Owner

@jwilsson commented on GitHub (May 4, 2020):

Awesome, glad we got it working!

but I'm curious why this one would manifest as an Error 500 rather than displaying the error more directly, like it does with those extra error reporting settings. (Perhaps different error reporting settings on the real server vs my XAMPP localhost?)

I think that's the case, different error reporting settings. Because it's throwing an exception, the default would be to trickle up and unless something catches it, it would eventually become a 500 error.

<!-- gh-comment-id:623251607 --> @jwilsson commented on GitHub (May 4, 2020): Awesome, glad we got it working! > but I'm curious why this one would manifest as an Error 500 rather than displaying the error more directly, like it does with those extra error reporting settings. (Perhaps different error reporting settings on the real server vs my XAMPP localhost?) I think that's the case, different error reporting settings. Because it's throwing an exception, the default would be to trickle up and unless something catches it, it would eventually become a 500 error.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#124
No description provided.