1

我正在尝试找出paypal 自适应支付的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"

进入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" }

我在搞砸什么?任何指针、指令或提示?我已经研究了三天的文档,但是很干,而且似乎没有好的教程存在。谢谢。

4

2 回答 2

5

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

它现在给出了它应该给出的答案。几天后,当我再次感到难以置信的困惑时,我会回来;)贝宝 API 很烂。

于 2013-10-06T17:54:50.683 回答
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);

    }

于 2021-03-04T14:20:50.753 回答