jwilsson / spotify-web-api-php

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

HTTP ERROR 500 with requestAccessToken #208

Closed AndryGabry01 closed 3 years ago

AndryGabry01 commented 3 years ago

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);
?>
jwilsson commented 3 years ago

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

Thanks!

AndryGabry01 commented 3 years ago

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

jwilsson commented 3 years ago

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.

AndryGabry01 commented 3 years ago

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

AndryGabry01 commented 3 years ago

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

jwilsson commented 3 years ago

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.

AndryGabry01 commented 3 years ago

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.