我正在嘗試OAuth 2.0使用以下指南獲取 Spotify API 的令牌:
https ://developer.spotify.com/documentation/general/guides/authorization/client-credentials/
我有以下 PHP 代碼:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.spotify.com/api/token');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic ' . base64_encode('MY_CLIENT_CODE:MY_CLIENT_SECRET')
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'grant_type' => 'client_credentials'
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
print_r($result);
但結果是:
Array ( [error] => unsupported_grant_type [error_description] => grant_type parameter is missing )
他們的示例代碼是 JavaScript,顯然我對它的理解不夠好,無法將其翻譯成 PHP。有人知道我做錯了什么嗎?
uj5u.com熱心網友回復:
您正在對表單欄位進行 json 編碼,您應該構建一個查詢引數。
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'grant_type' => 'client_credentials'
]));
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/439736.html
