I know this may sound simple to those of you who are well versed in OAuth 2.0, but I am still trying to understand it all. I am having to migrate my Xero OAuth 1.0 app to Xero OAuth 2.0. I am able to connect and get back a token. I am saving the refresh token in the database. My problem is I do not know when to use it. I have been trying to figure out how to test if the current access_token is expired so that I can refresh if needed, but I can't figure out how to test if the access_token has expired. I am getting and storing the token as follows:
var token = await xeroClient.RequestXeroTokenAsync(oauth_token);
xeroToken = new XeroOAuth2Token
{
AccessToken = token.AccessToken,
RefreshToken = token.RefreshToken,
ExpiresAtUtc = token.ExpiresAtUtc
};
Is there a way to test if the AccessToken has expired so that I know to call the RefreshTokenAsync method?
When you store the token in your data store, store it with the ExpiresAtUtc DateTime property. Then its just as simple as checking whether the current DateTime.UtcNow > token.ExpiresAtUtc.