Search code examples
phppaypalpaypal-sandboxpaypal-rest-sdkpaypal-connect

Connect With PayPal : Get status 400 (Bad Request)


I am facing 400 bad request response from PayPal during getting access token based on access code. I go through below reference link https://developer.paypal.com/docs/connect-with-paypal/integrate/#1-create-the-app

I follow 5 steps perfectly but getting the issue with step no. 6. See my code below:

Request Part:

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

Response Part:

stdClass Object (
    [name] => Bad Request
    [debug_id] => 5837077aa8787
    [message] => java.lang.IllegalArgumentException
    [details] => Array
        (
        )
)

Solution

  • Resolve issue see my below code:

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