cURL请求Spotify API意外状态:415

时间:2015-03-09 13:25:54

标签: php curl spotify

这是我的Spotify验证请求的脚本,但它返回错误。我尝试更改内容类型,但这似乎并没有削减它。这是我的代码:

    $spot_api_client = 'client';
    $spot_api_secret = 'secret';
    $spot_api_redirect = 'myurl';

    if(isset($_GET['state']) && isset($_COOKIE['stateKey']) && $_COOKIE['stateKey'] == $_GET['state']){

        $ch = curl_init();
        $curlConfig = array(
           CURLOPT_URL            => "https://accounts.spotify.com/api/token",
           CURLOPT_POST           => true,
           CURLOPT_RETURNTRANSFER => true,
           CURLOPT_POSTFIELDS     => array(
               'grant_type' => 'authorization_code',
               'code' => $_GET['code'],
               'redirect_uri' => urlencode($spot_api_redirect),
           ),
           CURLOPT_HTTPHEADER     => array(
               'Accept' => '*/*',
               'User-Agent' => 'runscope/0.1',
               'Authorization' => 'Basic '. base64_encode($spot_api_client.':'.$spot_api_secret), 
               'Content-Type'=>'application/json'
           )
       );
       curl_setopt_array($ch, $curlConfig);
       $result = curl_exec($ch);
       curl_close($ch);

       print_r($result);

   }

2 个答案:

答案 0 :(得分:4)

好吧,我似乎找到了基于this question的答案:

    $url = 'https://accounts.spotify.com/api/token';
    $method = 'POST';
    $spot_api_redirect = 'myurl';

    $credentials = "client:secret";

    $headers = array(
            "Accept: */*",
            "Content-Type: application/x-www-form-urlencoded",
            "User-Agent: runscope/0.1",
            "Authorization: Basic " . base64_encode($credentials));

    $data = 'grant_type=authorization_code&code='.$_GET['code'].'&redirect_uri='.urlencode($spot_api_redirect);

    if(isset($_GET['state']) && isset($_COOKIE['stateKey']) && $_COOKIE['stateKey'] == $_GET['state']){
        unset($_COOKIE['stateKey']);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = json_decode(curl_exec($ch), true);
        curl_close($ch);
        print_r($response);

    }

答案 1 :(得分:2)

如果使用node.js / request,请指出“WTF”标签:)

request(
    {
        method : "POST",
        url : "https://accounts.spotify.com/api/token",
        json : true,
        headers : 
        {
            "Content-Type" : "application/x-www-form-urlencoded", // WTF!
            "Authorization" : "Basic " + new Buffer(client_id+":"+client_secret).toString('base64')
        },
        body : "grant_type=client_credentials"
    },
    function (err, response, body)
    {
        if (err)
        {
            cb(err);
        }
        else
        {
            if (body.hasOwnProperty("access_token"))
            {
                access_token = body.access_token;
                cb(null);
            }
            else
            {
                cb(body);
            }
        }
    }
);
相关问题