PayPal Rest SDK API - Get Token

3.1k views Asked by At

I am truly stumped here; trying to use the PayPal REST api and cannot even get the access token. When using CURL (example), the token is created fine so I know the Client ID and Secret are OK.

When I use the code below with the PHP libraries, I get null's for access token in JSON Response.

require_once('/home/admin/vendor/autoload.php');

$host = 'https://api.sandbox.paypal.com';
$clientId = <CLIENT_ID>;
$clientSecret = <CLIENT_SECRET>;

use PayPal\Auth\OAuthTokenCredential;

$sdkConfig = array(
    "mode" => "sandbox"
);

$cred = new OAuthTokenCredential(
    $clientId,
    $clientSecret,
    $sdkConfig
);

var_dump($cred);

var_dump JSON Response: "accessToken":"PayPal\Auth\OAuthTokenCredential":private]=> NULL

1

There are 1 answers

0
Grokify On BEST ANSWER

From looking at the SDK code, the OAuthTokenCredential constructor method doesn't take a $config parameter but the getAccessToken() method does.

Here are the methods from the OAuthTokenCredential class code:

public function __construct($clientId, $clientSecret) { ... }

public function getAccessToken($config) { ... }

The following should get an access token for you:

$clientId = <CLIENT_ID>;
$clientSecret = <CLIENT_SECRET>;

use PayPal\Auth\OAuthTokenCredential;

$sdkConfig = array(
    "mode" => "sandbox"
);

$cred = new OAuthTokenCredential(
    $clientId,
    $clientSecret
);

$access_token = $cred->getAccessToken($sdkConfig);

echo $access_token;