Closed AndryGabry01 closed 3 years ago
Hi! A few questions to help me understand what's going on:
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';
)$_GET['code']
parameter will exist?require
) and see if you're getting more detailed errors:
error_reporting(-1);
ini_set('display_errors', 1);
Thanks!
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
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.
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
i tried to re download the api with composer, but script continue give me error
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.
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.
when i call the requestAccessToken method return me a HTTP ERROR 500