卷曲错误:不支持的grant_type

时间:2013-10-05 21:58:18

标签: php json curl paypal header

我试图弄清楚API documentation的paypal自适应付款。所以我试图翻译这个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"

进入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);

打印:

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

我搞砸了什么?任何指针,指令或提示?我现在已经研究了三天的文档,但它非常干燥,似乎没有好的教程。感谢。

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

我遇到了同样的问题,这是我获得令牌的方式

public function getToken(){

        $curl = curl_init("https://api-m.sandbox.paypal.com/v1/oauth2/token");

        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "grant_type=client_credentials",
            CURLOPT_HTTPHEADER => array(
                "Content-Type: application/json",
                "Authorization: Basic ". base64_encode("$clientApi:$clientSecret")
            ),
        ));

        $response = curl_exec($curl);
        $no_json = json_decode($response, true);
        if (!curl_errno($curl)) {

            switch ($http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE)) {
                case 200:
                    return $no_json['access_token'];
                    break;
                default:
                    return $no_json;
            }
        }

        curl_close($curl);

    }