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

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

'  Read the metadata saved by Get a sandbox access token.
set timing = CreateObject("Chilkat.JsonObject")
set issued = CreateObject("Chilkat.CkDateTime")
loadedTiming = timing.LoadFile("paypal-token-timing.json")
If (loadedTiming <> 1) Then
    outFile.WriteLine("No timing metadata. Request a token.")
    WScript.Quit
End If

validTimestamp = issued.SetFromTimestamp(timing.StringOf("issued_at"))
If (validTimestamp <> 1) Then
    outFile.WriteLine("Invalid issue time. Request a token.")
    WScript.Quit
End If

set now = CreateObject("Chilkat.CkDateTime")
success = now.SetFromCurrentSystemTime()
age = now.DiffSeconds(issued)
lifetime = timing.IntOf("expires_in")
If ((age < 0) Or (lifetime <= 300) Or (age >= (lifetime - 300))) Then
    outFile.WriteLine("Request a new client-credentials token.")
    WScript.Quit
End If

outFile.WriteLine("Reuse the matching token from the credential store.")

outFile.Close