C++ Requires Chilkat v11.0.0+
C++
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 C++ Downloads
#include <CkJsonObject.h>
#include <CkDateTime.h>
void ChilkatSample(void)
{
// Read the metadata saved by Get a sandbox access token.
CkJsonObject timing;
CkDateTime issued;
bool loadedTiming = timing.LoadFile("paypal-token-timing.json");
if (loadedTiming != true) {
std::cout << "No timing metadata. Request a token." << "\r\n";
return;
}
bool validTimestamp = issued.SetFromTimestamp(timing.stringOf("issued_at"));
if (validTimestamp != true) {
std::cout << "Invalid issue time. Request a token." << "\r\n";
return;
}
CkDateTime now;
now.SetFromCurrentSystemTime();
int age = now.DiffSeconds(issued);
int lifetime = timing.IntOf("expires_in");
if ((age < 0) || (lifetime <= 300) || (age >= (lifetime - 300))) {
std::cout << "Request a new client-credentials token." << "\r\n";
return;
}
std::cout << "Reuse the matching token from the credential store." << "\r\n";
}