Can't call the https://api.sandbox.paypal.com/v1/oauth2/token

lsmpascal
Contributor
Contributor

Hi.
I'm trying to call the https://api.sandbox.paypal.com/v1/oauth2/token to simply have a token.
I'm using php curl.

 

Here is my code : 

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sandbox.paypal.com/v1/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",

CURLOPT_USERPWD => $PAYPAL_CLIENT_ID.":".$PAYPAL_SECRET,

CURLOPT_POSTFIELDS => "{\"grant_type\":\"client_credentials\"}",

CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Accept-Language: en_US"
),
));

 

 

The answer is : {"error":"unsupported_grant_type","error_description":"Grant Type is NULL"}
I checked and I simply use the grant type i've found here : https://developer.paypal.com/docs/api/get-an-access-token-curl/

 

I'm totally lost with the error, because it's the very beginning of my integration.

Thank you.

 

Login to Me Too
1 ACCEPTED SOLUTION

Accepted Solutions
Solved

MTS_Justin
Moderator
Moderator
Hello,

Your attempting to pass the item as a JSON object, which it's not.

It should be :

CURLOPT_POSTFIELDS => "grant_type=client_credentials",

Thanks !

Was my post helpful? If so, please give me a kudos!

View solution in original post

Login to Me Too
6 REPLIES 6
Solved

MTS_Justin
Moderator
Moderator
Hello,

Your attempting to pass the item as a JSON object, which it's not.

It should be :

CURLOPT_POSTFIELDS => "grant_type=client_credentials",

Thanks !

Was my post helpful? If so, please give me a kudos!
Login to Me Too

lsmpascal
Contributor
Contributor

Hi. 

I don't know if your answer (for which I thank you !) is the solution, because after implementing it, I have another error : "NSS: client certificate not found (nickname not specified)".
Do you have any idear on this? 

Login to Me Too

MTS_Justin
Moderator
Moderator

Hello,

So if after adding the code I provided you're no longer experiencing the same error, then the code fixed the original issue.

This is confirmed by the new error you're experiencing, as whilst connecting to the sandbox endpoint a matching certificate for the endpoint cannot be found within the NSS store on your local machine.

You will need to add the sandbox certificate (api.sandbox.paypal.com) into your NSS store.

How to add a certificate into a NSS store : https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/tools/NSS_Tools_certutil

PayPal sandbox API certificates download : https://www.paypal.com/us/smarthelp/article/where-can-i-find-ssl-certificates-for-paypal-sandbox-api...

Thanks !


Was my post helpful? If so, please give me a kudos!
Login to Me Too

lsmpascal
Contributor
Contributor

I love detailed answers like yours.

Thank you so much !

Login to Me Too

prlama
Member
Member

Please help. Requesting by following giving me `Bad request`.

curl -X POST https://api.sandbox.paypal.com/v1/oauth2/token \ -H 'Authorization: Basic {Your Base64-encoded ClientID:Secret}=' \ -d 'grant_type=authorization_code&code={authorization_code}'

 

I encrypted `Base64-encoded ClientID:Secret` by btoa. authorization_code is used by capturing from redirect_url

Login to Me Too

managepavans
Contributor
Contributor

Hi,

 

I am also facing the same issue with getting access token at PayPal connect process. See code below:

Request Code:

$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' => '{CODE}'
),
CURLOPT_HTTPHEADER => array(
"Authorization: Basic ".$base_token,
),
);
curl_setopt_array($curl,$data );
$response = curl_exec($curl);
curl_close($curl);

Response Code:

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

 

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.