dream890110 2013-10-05 21:58
浏览 499
已采纳

卷曲错误:不支持的grant_type

I'm trying to figure out the API documentation for paypal adaptive payments. So I'm trying to translate this curl command(the example):

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"

into php(the only thing not shown is my clientID and secret):

$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);

this prints:

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

What am I messing up? Any pointers, directives or tips? I've been studying the documentation for three days now, but it's very dry, and no good tutorials seem to exist. Thanks.

  • 写回答

1条回答 默认 最新

  • dongzhuang6177 2013-10-06 17:54
    关注

    I'm not sure why, and I'd still accept any answer that can explain why, but replacing json_encode($data) with "grant_type=client_credentials" fixed my problem. I guess I'm still confused about JSON, because it appears to work without that.

    It's now giving the answer it is supposed to give. I'll be back in a couple days when I'm incredibly confused again ;) the paypal API sucks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵