1

I'm trying to allow users to login to my site using their spotify account but receive

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

when receiving the JSON response.

This is the code I'm using:

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;

I'm at number 4 in the spotify documentation... https://developer.spotify.com/web-api/authorization-guide/

Thanks

fusion3k
  • 11,568
  • 4
  • 25
  • 47

1 Answers1

0

Most likely the error is due to the form data being posted as multipart/form-data (because you are passing an array to CURLOPT_POSTFIELDS).

Instead, try:

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

This way it POSTs data as application/x-www-form-urlencoded.

drew010
  • 68,777
  • 11
  • 134
  • 162