卷曲错误:不支持的 grant_type

Jam*_*uld 1 php json curl paypal header

我正在尝试找出贝宝自适应支付的API 文档。所以我试图翻译这个 curl 命令(示例):

curl https://api.sandbox.paypal.com/v1/oauth2/token \
 -H "Accept: application/json" \
 -H "Accept-Language: en_US" \
 -u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
 -d "grant_type=client_credentials"
Run Code Online (Sandbox Code Playgroud)

进入php(唯一没有显示的是我的clientID和秘密):

$data =
    'client_id=' . $clientID . '&' .
    'client_secret=' . $clientSecret . '&' .
    "grant_type=client_credentials";

$url = "https://api.sandbox.paypal.com/v1/oauth2/token";
$headers = array(
    'Accept' => 'application/json',
    'Accept-Language' => 'en_US',
    "grant_type=client_credentials"
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, $clientID . ':' . $clientSecret);
$x = json_decode(curl_exec($ch));

var_dump($x);
Run Code Online (Sandbox Code Playgroud)

这打印:

object(stdClass)#1 (2) { ["error"]=> string(22) "unsupported_grant_type" ["error_description"]=> string(22) "Unsupported grant_type" }
Run Code Online (Sandbox Code Playgroud)

我在捣乱什么?任何指示、指令或提示?我已经研究了三天的文档,但它很枯燥,似乎没有好的教程存在。谢谢。

Jam*_*uld 5

我不确定为什么,我仍然会接受任何可以解释原因的答案,但替换json_encode($data)"grant_type=client_credentials"解决了我的问题。我想我仍然对 JSON 感到困惑,因为它似乎没有它就可以工作。

它现在给出了它应该给出的答案。当我再次感到非常困惑时,我将在几天后回来 ;) paypal API 很烂。