Sample code for 30+ languages & platforms
Rust 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 Rust Downloads

Rust
// Read the metadata saved by Get a sandbox access token.
let timing = chilkat::JsonObject::new();
let issued = chilkat::DateTime::new();
if timing.load_file("paypal-token-timing.json").is_err() {
    println!("No timing metadata. Request a token.");
    return;
}

if issued.set_from_timestamp(&timing.string_of("issued_at").unwrap_or_default()).is_err() {
    println!("Invalid issue time. Request a token.");
    return;
}

let now = chilkat::DateTime::new();
let _ = now.set_from_current_system_time();
let age = now.diff_seconds(&issued);
let lifetime = timing.int_of("expires_in");
if (age < 0) || ((lifetime <= 300) || (age >= (lifetime - 300))) {
    println!("Request a new client-credentials token.");
    return;
}

println!("Reuse the matching token from the credential store.");