Spotify Oauth,获取用户资料

Spotify Oauth, Getting users profile

我试图让用户使用他们的 spotify 帐户登录我的网站,但收到

{"error":"server_error","error_description":"Unexpected status: 415"}

收到 JSON 响应时。

这是我正在使用的代码:

if (isset($_GET['code'])  ) {
    echo $_GET['code'];
    $scode = $_GET['code'];
    $surl2 = "https://accounts.spotify.com/api/token";
    $sdata2=array("grant_type"=>"authorization_code","code"=>$scode,"redirect_uri"=>$sredicturl,"client_id"=>$Sclient,"client_secret"=>$Ssecret);
    $sch2=curl_init($surl2);
    curl_setopt($sch2,CURLOPT_POST,true);
    curl_setopt($sch2,CURLOPT_POSTFIELDS,$sdata2);
    curl_setopt($sch2,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($sch2,CURLOPT_RETURNTRANSFER,1);
    $sjson_response2=curl_exec($sch2);
    curl_close($sch2);
    echo"<br/>".$sjson_response2;

我在 spotify 文档中排名第 4...https://developer.spotify.com/web-api/authorization-guide/

谢谢

错误很可能是由于表单数据被发布为 multipart/form-data(因为您将数组传递给 CURLOPT_POSTFIELDS)。

相反,尝试:

curl_setopt($sch2,CURLOPT_POSTFIELDS, http_build_query($sdata2));

这样它将数据发布为 application/x-www-form-urlencoded