cURL 请求 Spotify API 意外状态:415

Posted

技术标签:

【中文标题】cURL 请求 Spotify API 意外状态:415【英文标题】:cURL request Spotify API Unexpected status: 415 【发布时间】:2015-05-10 16:13:42 【问题描述】:

这是我对 Spotify 的身份验证请求的脚本,但它返回错误。我尝试更改 Content-Type,但这似乎并没有减少它。这是我的代码:

    $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);

   

【问题讨论】:

【参考方案1】:

好吧,我似乎根据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);

    

【讨论】:

【参考方案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);
            
        
    
);

【讨论】:

以上是关于cURL 请求 Spotify API 意外状态:415的主要内容,如果未能解决你的问题,请参考以下文章

Spotify:发布到 api/token 给出了错误的请求,但在 curl 中工作

创建播放列表 spotify API

Spotify web api 在获取播放列表请求时返回状态 500

尝试使用其 API 在 Spotify 中创建播放列表时出现“请求失败,状态码为 401”错误

spotify api授权问题

我正在使用 php 通过 PHP 和客户端凭据流连接到 spotify 的 API