【发布时间】:2016-07-24 02:50:38
【问题描述】:
我正在构建我的第一个 Spotify 应用程序,现在我正在处理授权过程。
到目前为止,我已经成功地从https://accounts.spotify.com/authorize 检索到我的状态和代码
现在我通过 PHP CURL 请求发送 POST 请求以获取我的访问令牌。
Spotify's instructions for this step
我不断收到以下 JSON 错误响应,表明我的 grant_type 无效,它为我提供了三个有效选项:
{"error":"unsupported_grant_type","error_description":"grant_type 必须是 client_credentials、authorization_code 或 refresh_token"}bool(true)
如果您查看下面的代码,我相信我已经设置了正确的“authorization_code”授权类型,但我收到了错误。我用 '******' 突出显示了我认为正确的代码行的代码 sn-p。
谁能看到我做错了什么?这是我用来发送请求的代码:
// Get access tokens
$ch = curl_init();
// Specify the HTTP headers to send.
//Authorization: Basic <base64 encoded client_id:client_secret>
$ClientIDSpotify = "[my spotify app id]";
$ClientSecretSpotify = "[my secret code]";
$authorization = base64_encode ( "{$ClientIDSpotify}:{$ClientSecretSpotify}" );
$http_headers = array(
"Authorization: Basic {$authorization}"
);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $http_headers );
curl_setopt( $ch, CURLOPT_POST, true);
$spotify_url = "https://accounts.spotify.com/api/token";
curl_setopt( $ch, CURLOPT_URL, $spotify_url );
// *************************************************
// HERE'S WHERE I CORRECTLY SPECIFY THE GRANT TYPE
// *************************************************
$data['grant_type'] = "authorization_code";
$data['code'] = $authorizationCode;
$callbackURL = "[my callback URL]";
$data['redirect_uri'] = $callbackURL;
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response_json = curl_exec( $ch );
curl_close( $ch );
}
【问题讨论】:
标签: php curl authorization token spotify