Php 带有Spotify的OAuth无法使用curl获取刷新令牌
我一直在为一个项目创建自己的SpotifyAPI。我已经学习了WebAPI教程,但仍然无法获得刷新令牌的授权 此请求是在我成功获得第一次授权后立即提出的 有人能发现我的代码有问题吗Php 带有Spotify的OAuth无法使用curl获取刷新令牌,php,curl,oauth,spotify,Php,Curl,Oauth,Spotify,我一直在为一个项目创建自己的SpotifyAPI。我已经学习了WebAPI教程,但仍然无法获得刷新令牌的授权 此请求是在我成功获得第一次授权后立即提出的 有人能发现我的代码有问题吗 //Request for refresh token $url = "https://accounts.spotify.com/api/token"; $fieldString = ""; $fields = array( "grant_type" => "authorization_code",
//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));
//请求刷新令牌
$url=”https://accounts.spotify.com/api/token";
$fieldString=“”;
$fields=数组(
“授权类型”=>“授权代码”,
“code”=>$\u GET['code'],
“redirect_uri”=>$spotify->getRedirectURI()
//“client_id”=>$spotify->getClientID(),
//“client_secret”=>$spotify->getClientSecret()
);
$headers=数组(
“接受:应用程序/json”,
“内容类型:application/x-www-form-urlencoded”,
“授权:基本”.base64_编码($spotify->getClientID()。”:“$spotify->getClientSecret())
);
echo http_build_查询($fields)。“
”;
$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\u exec($ch);
卷曲关闭($ch);
var_dump(json_解码($response));
设置标题的正确选项是CURLOPT\u HTTPHEADER
,不幸的是,您使用的是CURLOPT\u标题
。这就是为什么您的授权头没有设置为curl请求
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
我收到不好的回复是因为我需要:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
在我插入那条线之后,我就可以获得我的代币了。希望这会有所帮助。由于响应为空,您可以使用verbose模式enable运行代码,并查看curl发送的内容是否与您尝试发送的内容完全相同<代码>curl_setopt($ch,CURLOPT_VERBOSE,true)代码>我仍然只收到一个“null”。除此之外什么也没有展出。