From cb6b0eb5e07d4277fa3defaf3675a4baac717f74 Mon Sep 17 00:00:00 2001 From: Michael Hallock Date: Tue, 15 Feb 2022 23:28:13 -0500 Subject: [PATCH] Adds an early token renewal to avoid possibility of token being valid at time of check, but invalid by the time an actual call is made less than a second later --- src/I8Beef.Ecobee/Client.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/I8Beef.Ecobee/Client.cs b/src/I8Beef.Ecobee/Client.cs index 90499c6..2a06f8f 100644 --- a/src/I8Beef.Ecobee/Client.cs +++ b/src/I8Beef.Ecobee/Client.cs @@ -190,7 +190,8 @@ await _tokenReadLock.WaitAsync() throw new NullReferenceException("Auth token storage delegate failed to provide token."); } - if (DateTime.Compare(DateTime.Now, storedAuthToken.TokenExpiration) >= 0) + // If token expires within 30 seconds, renew to avoid possible subsecond expiration + if (DateTime.Compare(DateTime.Now.AddSeconds(30), storedAuthToken.TokenExpiration) >= 0) { var requestMessage = new HttpRequestMessage(HttpMethod.Post, _baseUri + "token?grant_type=refresh_token&refresh_token=" + storedAuthToken.RefreshToken + "&client_id=" + _appKey); requestMessage.Headers.ExpectContinue = false;