Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.0.0+

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.

PayPal documentation

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkDateTimeW.h>

void ChilkatSample(void)
    {
    //  Read the metadata saved by Get a sandbox access token.
    CkJsonObjectW timing;
    CkDateTimeW issued;
    bool loadedTiming = timing.LoadFile(L"paypal-token-timing.json");
    if (loadedTiming != true) {
        wprintf(L"No timing metadata. Request a token.\n");
        return;
    }

    bool validTimestamp = issued.SetFromTimestamp(timing.stringOf(L"issued_at"));
    if (validTimestamp != true) {
        wprintf(L"Invalid issue time. Request a token.\n");
        return;
    }

    CkDateTimeW now;
    now.SetFromCurrentSystemTime();
    int age = now.DiffSeconds(issued);
    int lifetime = timing.IntOf(L"expires_in");
    if ((age < 0) || (lifetime <= 300) || (age >= (lifetime - 300))) {
        wprintf(L"Request a new client-credentials token.\n");
        return;
    }

    wprintf(L"Reuse the matching token from the credential store.\n");
    }