Handling Access Token Expiration

The JWT access token is only valid for a finite period of time.

Using an expired JWT will cause operations to fail. As you saw above, we are told how long a token is valid through expires_in. This value is normally 1200 seconds or 20 minutes. Expired tokens are not refreshed. We just fetch a new token and pass it to the Live Experience SDK.

You need to track the JWT expiration yourself. One way is to set a timer in your app to fetch a new token after 1200 seconds. Here is a timer in Swift:
Timer.scheduledTimer(timeInterval: expires, target: self, selector: #selector(fetchToken), userInfo: nil, repeats: false)

Another is to compute an expiration time by adding 1200 seconds to the current system time when the token was retrieved. So, if the time when you fetch the token was 10:00 the expiration time would be 10:20. An example of this in Swift looks like: let expireTime = Date().addingTimeInterval(expires).

You then compare the current time to the expiration time to see if the token has expired.

Twenty minutes is a long time and you may launch your app several times during that period. Storing your token in persistent storage allows it to survive after the app exits. Here is an example of storing a token and its expiration time in persistent storage with Swift:
UserDefaults.standard.set(token, forKey: "LXtoken")
UserDefaults.standard.set(expireTime, forKey: "tokenExpireTime")