Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
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.
Chilkat Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.CkDateTime,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
timing: TJsonObject;
issued: TCkDateTime;
loadedTiming: Boolean;
validTimestamp: Boolean;
now: TCkDateTime;
age: Integer;
lifetime: Integer;
begin
// Read the metadata saved by Get a sandbox access token.
timing := TJsonObject.Create;
issued := TCkDateTime.Create;
loadedTiming := timing.LoadFile('paypal-token-timing.json');
if (loadedTiming <> True) then
begin
WriteLn('No timing metadata. Request a token.');
Exit;
end;
validTimestamp := issued.SetFromTimestamp(timing.StringOf('issued_at'));
if (validTimestamp <> True) then
begin
WriteLn('Invalid issue time. Request a token.');
Exit;
end;
now := TCkDateTime.Create;
now.SetFromCurrentSystemTime();
age := now.DiffSeconds(issued);
lifetime := timing.IntOf('expires_in');
if ((age < 0) or (lifetime <= 300) or (age >= (lifetime - 300))) then
begin
WriteLn('Request a new client-credentials token.');
Exit;
end;
WriteLn('Reuse the matching token from the credential store.');
timing.Free;
issued.Free;
now.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.