cURL request Spotify API Unexpected status: 415
Well, seems I found the answer based on 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); }
Just in case of node.js / request use, point out "WTF" tag :)
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); } } });