【问题标题】:OAuth with Spotify unable to get refresh token using curl带有 Spotify 的 OAuth 无法使用 curl 获取刷新令牌
【发布时间】:2016-05-08 17:41:03
【问题描述】:

我一直在为一个项目创建自己的 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));

【问题讨论】:

    标签: php curl oauth spotify


    【解决方案1】:

    我收到不好回复的原因是因为我需要:

     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    

    在我插入那行之后,我能够获得我的令牌。希望这会有所帮助。

    【讨论】:

      【解决方案2】:

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

      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      

      【讨论】:

      • 我仍然得到 null 作为响应
      • 您可以在启用详细模式的情况下运行代码,并查看 curl 发送的内容是否与您尝试发送的内容完全相同。 curl_setopt($ch, CURLOPT_VERBOSE, true);
      • 我仍然只收到'null'。没有显示其他内容。
      猜你喜欢
      • 1970-01-01
      • 2016-07-10
      • 2021-02-21
      • 2015-10-11
      • 2019-10-26
      • 2015-11-26
      • 2021-01-18
      • 2014-05-03
      • 2022-12-03
      相关资源
      最近更新 更多