Sample code for 30+ languages & platforms
Zig

Check PayPal Access Token Expiration

See more PayPal Examples

Checks to see if the PayPal access token expired.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main() !void {
    // Note: Requires Chilkat v9.5.0.64 or greater.

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // In the example linked here: PayPal OAuth2 Token,
    // we fetched a PayPal access token and saved it to a JSON file.
    // We also recorded the current date/time.
    // Load this JSON file and compare the current date/time with the fetch date/time
    // and the "expires_in" value to see if the token is expired.

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.loadFile("qa_data/tokens/paypal.json") catch {
        std.debug.print("Failed to load access key json file.\n", .{});
        return;
    };

    // Get the current date/time.
    const date_time = try chilkat.DateTime.init();
    defer date_time.deinit();
    const b_local_time = false;
    const dt_now = @as(i32, @intCast(date_time.getAsUnixTime(b_local_time)));

    // Get the access token create date/time
    const dt_create = json.intOf("tokenCreateTimeUtc");

    // Find out how many seconds have elapsed.
    const num_seconds = dt_now - dt_create;

    // Get the expires_in value from the JSON.
    const expires_in = json.intOf("expires_in");

    std.debug.print("token age (in seconds) = {d}\n", .{num_seconds});
    std.debug.print("expires_in = {d}\n", .{expires_in});

    // If the token expired, or if it will expire in 5 minutes or less,
    // get another token.
    if (num_seconds < (expires_in - 300)) {
        std.debug.print("The token is not yet expired.  No need to fetch another.\n", .{});
        return;
    }

    std.debug.print("Time to fetch a new access token...\n", .{});

    // Get another token..
    // See the example at Get PayPal OAuth2 Token
    // to get another token..
}