Perl Requires Chilkat v11.0.0+
Perl
PayPal: Decide when to renew an access token
See more PayPal Examples
Use the UTC request time and expires_in from the token example. Renew five minutes early. This is a cache decision; a revoked token can still receive HTTP 401 before its calculated expiry.
Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
# Read the metadata saved by Get a sandbox access token.
my $timing = Chilkat2::JsonObject->new;
my $issued = Chilkat2::DateTime->new;
my $loadedTiming = $timing->LoadFile('paypal-token-timing.json');
if (!$loadedTiming) {
print "No timing metadata. Request a token.\n";
exit;
}
my $validTimestamp = $issued->SetFromTimestamp($timing->StringOf('issued_at'));
if (!$validTimestamp) {
print "Invalid issue time. Request a token.\n";
exit;
}
my $now = Chilkat2::DateTime->new;
$now->SetFromCurrentSystemTime();
my $age = $now->DiffSeconds($issued);
my $lifetime = $timing->IntOf('expires_in');
if (($age < 0) || (($lifetime <= 300) || ($age >= ($lifetime - 300)))) {
print "Request a new client-credentials token.\n";
exit;
}
print "Reuse the matching token from the credential store.\n";