Plan: Free
Country: United Kingdom
Device: Surface Pro 3
Operating System: Windows 10 (21H2) using Brave browser
My Question or Issue:
I believe I have successfully authenticated to the API using this code:
/* Get Spotify Authorization Token */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.spotify.com/api/token' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials' );
$headers = array();
$headers[] = 'Authorization: Basic '.base64_encode($client_id.':'.$client_secret);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ( !CURL_EXEC( $ch ) ){
exit( 'Error: ' . CURL_ERROR( $ch ) . ' - Code: ' .CURL_ERRNO( $ch ));
}
$auth_response_json = CURL_EXEC( $ch );
CURL_CLOSE( $ch );
$auth_response = json_decode( $auth_response_json, true );
if ($auth_response['access_token'] != ""){
print_r($auth_response);
echo('<hr>');
echo "<script>console.log('Authentication successful!' );</script>";
$auth=1;
$access_token = $auth_response['access_token'];
}
else {
echo "<script>console.log('Authentication failed!' );</script>";
$auth=0;
}
I am receiving an [access_token] in my response that looks like this:
Array ( [access_token] => BQAy-1lTVzXAQxvkq_LLchAjOJbPrBTAyHMUsbfYutP0xDopzy4_lyJzSK6tUn74qlGGez2jAcswga6UckOaEiTkyBrj1hGu-jL2Ce11aGZ1ITYjSRI [token_type] => Bearer [expires_in] => 3600 )
When I try to use the given [access_token] to request the /currently-playing track, I get a 404 error.
{ "error" : { "status" : 404, "message" : "Invalid username" } }
The code I am using to call the /currently-playing track is this:
if($auth=1){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/me/player/currently-playing' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Authorization: Bearer '.$access_token;
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($result);
}
else {
echo "<script>console.log('Not Authenticated, so cannot get current track.' );</script>";
echo("Ln: ". __LINE__);
}
I have also tried to call the /me/player status, with the same 404 result.
The Web API documentation makes no mention of a 404 status for the Get Currently Playing Track endpoint, and I can see no reference to a username parameter.