此请求需要身份验证。 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.
- Again double check the api key that is a valid.
- There is an extra
/
at the end of the endpointplaylist/
. It has a chance to cause the issue. You can try removing that/
after playlist.