与PayPal连接:获得状态400(坏请求)。

问题描述 投票:1回答:1

我在根据访问代码获取访问令牌的过程中遇到了来自PayPal的400个错误请求响应。我通过下面的参考链接 https:/developer.paypal.comdocsconnect-with-paypalintegrate#1-create-the-app。

我完全按照5个步骤来做,但在第6步时出现了问题。请看我下面的代码。

请求部分:

$curl = curl_init();

$base_token = "{client_id:secret}";

$data = array(
    CURLOPT_URL => "https://api.sandbox.paypal.com/v1/oauth2/token",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => array(
        'grant_type' => 'authorization_code',
        'code' => '{authorization_code}'
    ),
    CURLOPT_HTTPHEADER => array(
        "Authorization: Basic ".$base_token,
    ),
);
curl_setopt_array($curl,$data );
$response = curl_exec($curl);
curl_close($curl);

响应部分。

stdClass Object (
    [name] => Bad Request
    [debug_id] => 5837077aa8787
    [message] => java.lang.IllegalArgumentException
    [details] => Array
        (
        )
)
php paypal paypal-sandbox paypal-rest-sdk paypal-connect
1个回答
0
投票

传递一个数组给CURLOPT_POSTFIELDS会将内容类型设置为 multipart/form-data. 这是不对的,你需要向CURLOPT_POSTFIELDS传递一个查询字符串。

你需要给CURLOPT_POSTFIELDS传递一个查询字符串。

CURLOPT_POSTFIELDS => http_build_query(array(
        'grant_type' => 'authorization_code',
        'code' => '{authorization_code}'
    )),

0
投票

解决问题请看我下面的代码。

$base_token = '{client_id:secret}';

$curl = curl_init();

$data = array(
    CURLOPT_URL => "https://api.sandbox.paypal.com/v1/oauth2/token",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_POSTFIELDS => http_build_query(array(
        'grant_type'=>'authorization_code',
        'code'=> $request->code,
    )),
    CURLOPT_HTTPHEADER => array(
        "Content-Type: application/x-www-form-urlencoded",
    ),
    CURLOPT_USERPWD => $base_token
);

curl_setopt_array($curl, $data);

$response = curl_exec($curl);
curl_close($curl);
© www.soinside.com 2019 - 2024. All rights reserved.