CkPerl Requires Chilkat v11.0.0+
CkPerl
PayPal: Get a sandbox access token
See more PayPal Examples
Exchange a sandbox REST app client ID and secret for a bearer token on a merchant-controlled backoffice workstation. PayPal supplies its lifetime in seconds. Store the token in a protected credential store; the companion expiry example reads only timing metadata.
Chilkat CkPerl Downloads
use chilkat();
$success = 0;
# See Unlock Chilkat before making requests.
$http = chilkat::CkHttp->new();
# Load these values from the credential store on the merchant backoffice workstation.
# Never embed the merchant secret in an app distributed to customers.
$http->put_Login("SANDBOX_CLIENT_ID");
$http->put_Password("SANDBOX_CLIENT_SECRET");
$http->put_BasicAuth(1);
$issued = chilkat::CkDateTime->new();
$issued->SetFromCurrentSystemTime();
$resp = chilkat::CkHttpResponse->new();
$success = $http->HttpStr("POST","https://api-m.sandbox.paypal.com/v1/oauth2/token","grant_type=client_credentials","utf-8","application/x-www-form-urlencoded",$resp);
if ($success != 1) {
print "No HTTP response: " . $http->lastErrorText() . "\r\n";
exit;
}
if (($resp->get_StatusCode() < 200) or ($resp->get_StatusCode() >= 300)) {
$errorInfo = chilkat::CkJsonObject->new();
$errorInfo->Load($resp->bodyStr());
print "HTTP " . $resp->get_StatusCode() . " " . $errorInfo->stringOf("error") . " " . $errorInfo->stringOf("error_description") . "\r\n";
print "PayPal debug ID: " . $errorInfo->stringOf("debug_id") . "\r\n";
exit;
}
$result = chilkat::CkJsonObject->new();
$parsed = $result->Load($resp->bodyStr());
if ($parsed != 1) {
print "Expected a JSON response; HTTP " . $resp->get_StatusCode() . "\r\n";
exit;
}
$accessToken = $result->stringOf("access_token");
$lifetime = $result->IntOf("expires_in");
if (($result->HasMember("access_token") != 1) or ($lifetime <= 0)) {
print "Missing token or lifetime." . "\r\n";
exit;
}
# Supply accessToken to Http.AuthToken. Never log the bearer token or client secret.
# Save only timing metadata here, relative to the start of the token request.
$timing = chilkat::CkJsonObject->new();
$timing->UpdateString("issued_at",$issued->getAsTimestamp(0));
$timing->UpdateInt("expires_in",$lifetime);
$savedTiming = $timing->WriteFile("paypal-token-timing.json");
if ($savedTiming != 1) {
print "Could not save token timing." . "\r\n";
exit;
}
print "Token received; lifetime in seconds: " . $lifetime . "\r\n";