Go Requires Chilkat v11.0.0+
Go
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 Go Downloads
// Read the metadata saved by Get a sandbox access token.
timing := chilkat.NewJsonObject()
issued := chilkat.NewCkDateTime()
loadedTiming := timing.LoadFile("paypal-token-timing.json")
if loadedTiming != true {
fmt.Println("No timing metadata. Request a token.")
timing.DisposeJsonObject()
issued.DisposeCkDateTime()
return
}
validTimestamp := issued.SetFromTimestamp(*timing.StringOf("issued_at"))
if validTimestamp != true {
fmt.Println("Invalid issue time. Request a token.")
timing.DisposeJsonObject()
issued.DisposeCkDateTime()
return
}
now := chilkat.NewCkDateTime()
now.SetFromCurrentSystemTime()
age := now.DiffSeconds(issued)
lifetime := timing.IntOf("expires_in")
if (age < 0) || (lifetime <= 300) || (age >= (lifetime - 300)) {
fmt.Println("Request a new client-credentials token.")
timing.DisposeJsonObject()
issued.DisposeCkDateTime()
now.DisposeCkDateTime()
return
}
fmt.Println("Reuse the matching token from the credential store.")
timing.DisposeJsonObject()
issued.DisposeCkDateTime()
now.DisposeCkDateTime()