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

PowerBuilder
integer li_rc
oleobject loo_Timing
oleobject loo_Issued
long li_LoadedTiming
long li_ValidTimestamp
oleobject loo_Now
long li_Age
long li_Lifetime

//  Read the metadata saved by Get a sandbox access token.
loo_Timing = create oleobject
li_rc = loo_Timing.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Timing
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Issued = create oleobject
li_rc = loo_Issued.ConnectToNewObject("Chilkat.CkDateTime")

li_LoadedTiming = loo_Timing.LoadFile("paypal-token-timing.json")
if li_LoadedTiming <> 1 then
    Write-Debug "No timing metadata. Request a token."
    destroy loo_Timing
    destroy loo_Issued
    return
end if

li_ValidTimestamp = loo_Issued.SetFromTimestamp(loo_Timing.StringOf("issued_at"))
if li_ValidTimestamp <> 1 then
    Write-Debug "Invalid issue time. Request a token."
    destroy loo_Timing
    destroy loo_Issued
    return
end if

loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")

loo_Now.SetFromCurrentSystemTime()
li_Age = loo_Now.DiffSeconds(loo_Issued)
li_Lifetime = loo_Timing.IntOf("expires_in")
if (li_Age < 0) OR (li_Lifetime <= 300) OR (li_Age >= (li_Lifetime - 300)) then
    Write-Debug "Request a new client-credentials token."
    destroy loo_Timing
    destroy loo_Issued
    destroy loo_Now
    return
end if

Write-Debug "Reuse the matching token from the credential store."


destroy loo_Timing
destroy loo_Issued
destroy loo_Now