Spotify错误代码403 - 此请求需要用户身份验证

时间:2016-01-15 13:41:37

标签: php curl spotify

我想通过php将曲目添加到我的PRIVATE Spotify播放列表中 - 但我还是不能完成它。我总是收到错误消息

  

错误代码403 - 此请求需要用户身份验证。

我在spotify api中不是好事。有人可以帮我吗?这是我的代码:

<?php
error_reporting(E_ALL);
$url = 'https://accounts.spotify.com/api/token';
$method = 'POST';

$credentials = "Client ID:Client Secret";

$headers = array(
        "Accept: */*",
        "Content-Type: application/x-www-form-urlencoded",
        "Authorization: Basic " . base64_encode($credentials));
$data = 'grant_type=client_credentials';

$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_1 = curl_exec($ch);

curl_close($ch);

$response_1 = json_decode($response_1, true);
var_dump($response_1);

$token = $response_1['access_token'];
echo $token."<br>";

$headers_2 = array(
        "Accept: application/json",
        "Content-Type: application/x-www-form-urlencoded",
        ('Authorization: Bearer ' . $token));

$url_2 =  'https://api.spotify.com/v1/users/example/playlists/playlist_example_id/tracks';
$postargs = '?uris=spotify%3Atrack%3A4xwB7i0OMsmYlQGGbtJllv';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url_2);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_2);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_2 = curl_exec($ch);

curl_close($ch);

$response_2 = json_decode($response_2, true);
var_dump($response_2);

0 个答案:

没有答案