此请求需要身份验证。 Spotify API

此请求需要身份验证。  Spotify API

问题描述:

I'm trying to get all of the user's playlists from spotify, but my code returns me error 401, This request requires authentication.

I have already got the Authorisation key which is set as $key. Here's my code.

$key='*KEYHERE*';

$userid='acorn3';

$ch = curl_init();
$url = "https://api.spotify.com/v1/users/" . $userid . "/playlists/";
echo $url . "<br>";
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_URL, $url);
$header = array('Accept: application/json', 'Authorization: Bearer '.$key);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
if($result === false)
{
    echo "Error Number:".curl_errno($ch)."<br>";
    echo "Error String:".curl_error($ch);
}
curl_close($ch);

I think I'm missing something simple here, but I can't see what.

Your code seems ok as per the spotify documentation. You can consider the following two to adjust your code.

  1. Again double check the api key that is a valid.
  2. There is an extra / at the end of the endpoint playlist/. It has a chance to cause the issue. You can try removing that / after playlist.