[GH-ISSUE #208] HTTP ERROR 500 with requestAccessToken #144

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

Originally created by @AndryGabry01 on GitHub (Nov 21, 2020).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/208

when i call the requestAccessToken method return me a HTTP ERROR 500

<?php
require 'SpotifyApi/vendor/autoload.php';

$session = new SpotifyWebAPI\Session('cid', 'csec', 'callbackurl');

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

// Set the code on the API wrapper
SpotifyWebAPI\SpotifyWebAPI::setAccessToken($accessToken);
$track = SpotifyWebAPI\SpotifyWebAPI::getTrack('7EjyzZcbLxW7PaaLua9Ksb');

print_r($track);
?>

Originally created by @AndryGabry01 on GitHub (Nov 21, 2020). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/208 when i call the requestAccessToken method return me a HTTP ERROR 500 ``` <?php require 'SpotifyApi/vendor/autoload.php'; $session = new SpotifyWebAPI\Session('cid', 'csec', 'callbackurl'); // Request a access token using the code from Spotify $session->requestAccessToken($_GET['code']); $accessToken = $session->getAccessToken(); // We're good to go! // Set the code on the API wrapper SpotifyWebAPI\SpotifyWebAPI::setAccessToken($accessToken); $track = SpotifyWebAPI\SpotifyWebAPI::getTrack('7EjyzZcbLxW7PaaLua9Ksb'); print_r($track); ?> ```
kerem 2026-02-27 19:26:17 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (Nov 22, 2020):

Hi!
A few questions to help me understand what's going on:

  • The require call looks a bit strange, have you installed the library via Composer or have you done it in some other way? (I was expecting something like require 'vendor/autoload.php';)
  • Is this code run as the callback URL? So the $_GET['code'] parameter will exist?
  • Can you add the following lines to the top of the file (before require) and see if you're getting more detailed errors:
error_reporting(-1);
ini_set('display_errors', 1);

Thanks!

<!-- gh-comment-id:731715026 --> @jwilsson commented on GitHub (Nov 22, 2020): Hi! A few questions to help me understand what's going on: * The `require` call looks a bit strange, have you installed the library via Composer or have you done it in some other way? (I was expecting something like `require 'vendor/autoload.php';`) * Is this code run as the callback URL? So the `$_GET['code']` parameter will exist? * Can you add the following lines to the top of the file (before `require`) and see if you're getting more detailed errors: ```php error_reporting(-1); ini_set('display_errors', 1); ``` Thanks!
Author
Owner

@AndryGabry01 commented on GitHub (Nov 22, 2020):

i have use composer, but in the SpotifyApi directory.
when user login spotify, he is returned to callback.php

<?php
error_reporting (- 1 );
ini_set ( 'display_errors' , 1 );
require 'SpotifyApi/vendor/autoload.php';

$session = new SpotifyWebAPI\Session('cid', 'csec', 'callbackurl');

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

// Set the code on the API wrapper
SpotifyWebAPI\SpotifyWebAPI::setAccessToken($accessToken);
$track = SpotifyWebAPI\SpotifyWebAPI::getTrack('7EjyzZcbLxW7PaaLua9Ksb');

print_r($track);
?>

Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIAuthException: Invalid client in /var/www/html/test/SpotifyApi/vendor/jwilsson/spotify-web-api-php/src/Request.php:65 Stack trace: #0 /var/www/html/test/SpotifyApi/vendor/jwilsson/spotify-web-api-php/src/Request.php(270): SpotifyWebAPI\Request->parseBody(Object(stdClass), 400) #1 /var/www/html/test/SpotifyApi/vendor/jwilsson/spotify-web-api-php/src/Request.php(115): SpotifyWebAPI\Request->send('POST', 'https://account...', 'client_id=cid&c...', Array) #2 /var/www/html/test/SpotifyApi/vendor/jwilsson/spotify-web-api-php/src/Session.php(235): SpotifyWebAPI\Request->account('POST', '/api/token', Array, Array) #3 /var/www/html/test/callback.php(9): SpotifyWebAPI\Session->requestAccessToken('AQCI-JJbxmWEClv...') #4 {main} thrown in /var/www/html/test/SpotifyApi/vendor/jwilsson/spotify-web-api-php/src/Request.php on line 65

<!-- gh-comment-id:731761057 --> @AndryGabry01 commented on GitHub (Nov 22, 2020): i have use composer, but in the SpotifyApi directory. when user login spotify, he is returned to callback.php ``` <?php error_reporting (- 1 ); ini_set ( 'display_errors' , 1 ); require 'SpotifyApi/vendor/autoload.php'; $session = new SpotifyWebAPI\Session('cid', 'csec', 'callbackurl'); // Request a access token using the code from Spotify $session->requestAccessToken($_GET['code']); $accessToken = $session->getAccessToken(); // We're good to go! // Set the code on the API wrapper SpotifyWebAPI\SpotifyWebAPI::setAccessToken($accessToken); $track = SpotifyWebAPI\SpotifyWebAPI::getTrack('7EjyzZcbLxW7PaaLua9Ksb'); print_r($track); ?> ``` `Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIAuthException: Invalid client in /var/www/html/test/SpotifyApi/vendor/jwilsson/spotify-web-api-php/src/Request.php:65 Stack trace: #0 /var/www/html/test/SpotifyApi/vendor/jwilsson/spotify-web-api-php/src/Request.php(270): SpotifyWebAPI\Request->parseBody(Object(stdClass), 400) #1 /var/www/html/test/SpotifyApi/vendor/jwilsson/spotify-web-api-php/src/Request.php(115): SpotifyWebAPI\Request->send('POST', 'https://account...', 'client_id=cid&c...', Array) #2 /var/www/html/test/SpotifyApi/vendor/jwilsson/spotify-web-api-php/src/Session.php(235): SpotifyWebAPI\Request->account('POST', '/api/token', Array, Array) #3 /var/www/html/test/callback.php(9): SpotifyWebAPI\Session->requestAccessToken('AQCI-JJbxmWEClv...') #4 {main} thrown in /var/www/html/test/SpotifyApi/vendor/jwilsson/spotify-web-api-php/src/Request.php on line 65`
Author
Owner

@jwilsson commented on GitHub (Nov 22, 2020):

Aha! You're getting a "Invalid client" error back from Spotify. I've seen this a few times before and the easiest solution seems to be to create a new Spotify app and use that instead.

<!-- gh-comment-id:731828355 --> @jwilsson commented on GitHub (Nov 22, 2020): Aha! You're getting a "Invalid client" error back from Spotify. I've seen this a few times before and the easiest solution seems to be to create a new Spotify app and use that instead.
Author
Owner

@AndryGabry01 commented on GitHub (Nov 22, 2020):

I tried but keep giving me that error, now for safety I will try to re-download your api with the composer.
I update you

<!-- gh-comment-id:731832195 --> @AndryGabry01 commented on GitHub (Nov 22, 2020): I tried but keep giving me that error, now for safety I will try to re-download your api with the composer. I update you
Author
Owner

@AndryGabry01 commented on GitHub (Nov 22, 2020):

i tried to re download the api with composer, but script continue give me error

<!-- gh-comment-id:731837245 --> @AndryGabry01 commented on GitHub (Nov 22, 2020): i tried to re download the api with composer, but script continue give me error
Author
Owner

@jwilsson commented on GitHub (Nov 23, 2020):

Hmm, try changing these lines:

// Set the code on the API wrapper
SpotifyWebAPI\SpotifyWebAPI::setAccessToken($accessToken);
$track = SpotifyWebAPI\SpotifyWebAPI::getTrack('7EjyzZcbLxW7PaaLua9Ksb');

to this:

// Set the code on the API wrapper
$api = new SpotifyWebAPI\SpotifyWebAPI();
$api->setAccessToken($accessToken);
$track = $api->getTrack('7EjyzZcbLxW7PaaLua9Ksb');

Right now you're calling the methods statically which means they won't have access to the access token. Creating an instance of the class and using that will make sure everything is set correctly.

<!-- gh-comment-id:731933840 --> @jwilsson commented on GitHub (Nov 23, 2020): Hmm, try changing these lines: ```php // Set the code on the API wrapper SpotifyWebAPI\SpotifyWebAPI::setAccessToken($accessToken); $track = SpotifyWebAPI\SpotifyWebAPI::getTrack('7EjyzZcbLxW7PaaLua9Ksb'); ``` to this: ```php // Set the code on the API wrapper $api = new SpotifyWebAPI\SpotifyWebAPI(); $api->setAccessToken($accessToken); $track = $api->getTrack('7EjyzZcbLxW7PaaLua9Ksb'); ``` Right now you're calling the methods statically which means they won't have access to the access token. Creating an instance of the class and using that will make sure everything is set correctly.
Author
Owner

@AndryGabry01 commented on GitHub (Nov 23, 2020):

hi, I managed to make everything work, I don't know what the problem was, probably my forgetfulness.
to fix it, I just went back to rewriting everything

in any case thank you very much for the help and sorry for the inconvenience.

<!-- gh-comment-id:732128161 --> @AndryGabry01 commented on GitHub (Nov 23, 2020): hi, I managed to make everything work, I don't know what the problem was, probably my forgetfulness. to fix it, I just went back to rewriting everything in any case thank you very much for the help and sorry for the inconvenience.
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#144
No description provided.