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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // Read the metadata saved by Get a sandbox access token.
  final timing = CkJsonObject();
  final issued = CkDateTime();
  try {
    timing.loadFile('paypal-token-timing.json');
  } on ChilkatException {
    print('No timing metadata. Request a token.');
    return;
  }

  try {
    issued.setFromTimestamp(timing.stringOf('issued_at'));
  } on ChilkatException {
    print('Invalid issue time. Request a token.');
    return;
  }

  final now = CkDateTime();
  now.setFromCurrentSystemTime();
  final age = now.diffSeconds(issued);
  final lifetime = timing.intOf('expires_in');
  if ((age < 0) || ((lifetime <= 300) || (age >= (lifetime - 300)))) {
    print('Request a new client-credentials token.');
    return;
  }

  print('Reuse the matching token from the credential store.');
}