Ruby Requires Chilkat v11.0.0+
Ruby
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 Ruby Downloads
require 'chilkat'
# Read the metadata saved by Get a sandbox access token.
timing = Chilkat::CkJsonObject.new()
issued = Chilkat::CkDateTime.new()
loadedTiming = timing.LoadFile("paypal-token-timing.json")
if (loadedTiming != true)
print "No timing metadata. Request a token." + "\n";
exit
end
validTimestamp = issued.SetFromTimestamp(timing.stringOf("issued_at"))
if (validTimestamp != true)
print "Invalid issue time. Request a token." + "\n";
exit
end
now = Chilkat::CkDateTime.new()
now.SetFromCurrentSystemTime()
age = now.DiffSeconds(issued)
lifetime = timing.IntOf("expires_in")
if ((age < 0) or (lifetime <= 300) or (age >= (lifetime - 300)))
print "Request a new client-credentials token." + "\n";
exit
end
print "Reuse the matching token from the credential store." + "\n";