带有 Spotify 的 OAuth 无法使用 curl 获取刷新令牌


OAuth with Spotify unable to get refresh token using curl

我一直在为一个项目创建自己的Spotify API。我已按照 Web API 教程进行操作,但仍然无法获得刷新令牌的授权。

此请求是在我成功获得第一次授权后立即提出的。

任何人都可以发现我的代码有问题吗?

//Request for refresh token
$url = "https://accounts.spotify.com/api/token";
$fieldString = "";
$fields = array(
    "grant_type" => "authorization_code",
    "code" => $_GET['code'],
    "redirect_uri" => $spotify->getRedirectURI()
    //"client_id" => $spotify->getClientID(),
    //"client_secret" => $spotify->getClientSecret()
);
$headers = array(
    "Accept: application/json",
    "Content-Type: application/x-www-form-urlencoded",
    "Authorization: Basic ".base64_encode($spotify->getClientID().":".$spotify->getClientSecret())
);
echo http_build_query($fields)."<br/>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
$response = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($response));

我收到错误回复的原因是因为我需要:

 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

插入该行后,我能够获得我的令牌。希望这是有帮助的。

设置标题的正确选项是CURLOPT_HTTPHEADER,不幸的是您使用的是CURLOPT_HEADER .这就是为什么您的授权标头未使用 curl 请求设置的原因。

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);