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

Objective-C
#import <CkoJsonObject.h>
#import <CkoDateTime.h>

//  Read the metadata saved by Get a sandbox access token.
CkoJsonObject *timing = [[CkoJsonObject alloc] init];
CkoDateTime *issued = [[CkoDateTime alloc] init];
BOOL loadedTiming = [timing LoadFile: @"paypal-token-timing.json"];
if (loadedTiming != YES) {
    NSLog(@"%@",@"No timing metadata. Request a token.");
    return;
}

BOOL validTimestamp = [issued SetFromTimestamp: [timing StringOf: @"issued_at"]];
if (validTimestamp != YES) {
    NSLog(@"%@",@"Invalid issue time. Request a token.");
    return;
}

CkoDateTime *now = [[CkoDateTime alloc] init];
[now SetFromCurrentSystemTime];
int age = [[now DiffSeconds: issued] intValue];
int lifetime = [[timing IntOf: @"expires_in"] intValue];
if ((age < 0) || (lifetime <= 300) || (age >= (lifetime - 300))) {
    NSLog(@"%@",@"Request a new client-credentials token.");
    return;
}

NSLog(@"%@",@"Reuse the matching token from the credential store.");