From f59f57f1c8827cb98f8b3e83193fb8f1b03f44cf Mon Sep 17 00:00:00 2001 From: Mark Hilbush Date: Fri, 5 Apr 2024 10:01:50 -0400 Subject: [PATCH] Improve handling of error code 14 (#16613) Signed-off-by: Mark Hilbush Signed-off-by: Ciprian Pascu --- .../ecobee/internal/api/EcobeeApi.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java b/bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java index 0f185c8100a..0085968ad76 100644 --- a/bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java +++ b/bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java @@ -83,6 +83,8 @@ public class EcobeeApi { private static final int ECOBEE_TOKEN_EXPIRED = 14; private static final int ECOBEE_DEAUTHORIZED_TOKEN = 16; private static final int TOKEN_EXPIRES_IN_BUFFER_SECONDS = 120; + private static final boolean FORCE_TOKEN_REFRESH = true; + private static final boolean DONT_FORCE_TOKEN_REFRESH = false; public static final Properties HTTP_HEADERS; static { @@ -147,14 +149,15 @@ public class EcobeeApi { * response, then assume that the Ecobee authorization process is complete. Otherwise, * start the Ecobee authorization process. */ - private boolean isAuthorized() { + private boolean isAuthorized(boolean forceTokenRefresh) { boolean isAuthorized = false; try { AccessTokenResponse localAccessTokenResponse = oAuthClientService.getAccessTokenResponse(); if (localAccessTokenResponse != null) { logger.trace("API: Got AccessTokenResponse from OAuth service: {}", localAccessTokenResponse); - if (localAccessTokenResponse.isExpired(Instant.now(), TOKEN_EXPIRES_IN_BUFFER_SECONDS)) { - logger.debug("API: Token is expiring soon. Refresh it now"); + if (forceTokenRefresh + || localAccessTokenResponse.isExpired(Instant.now(), TOKEN_EXPIRES_IN_BUFFER_SECONDS)) { + logger.debug("API: Refreshing access token"); localAccessTokenResponse = oAuthClientService.refreshToken(); } ecobeeAuth.setState(EcobeeAuthState.COMPLETE); @@ -187,6 +190,10 @@ public class EcobeeApi { return isAuthorized; } + private boolean isAuthorized() { + return isAuthorized(DONT_FORCE_TOKEN_REFRESH); + } + private void handleOAuthException(OAuthResponseException e) { if ("invalid_grant".equalsIgnoreCase(e.getError())) { // Usually indicates that the refresh token is no longer valid and will require reauthorization @@ -346,15 +353,13 @@ public class EcobeeApi { logger.debug("API: AccessTokenResponse created on: {}", localAccessTokenResponse.getCreatedOn()); logger.debug("API: AccessTokenResponse expires in: {}", localAccessTokenResponse.getExpiresIn()); } - // Recreating the OAuthClientService seems to be the only way to handle this error - closeOAuthClientService(); - createOAuthClientService(); - if (isAuthorized()) { + logger.debug("API: Ecobee API attempting to force an access token refresh"); + if (isAuthorized(FORCE_TOKEN_REFRESH)) { return true; } else { - logger.warn("API: isAuthorized was NOT successful on second try"); + logger.warn("API: isAuthorized was NOT successful forcing the access token refresh"); bridgeHandler.updateBridgeStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, - "Unable to refresh access token"); + "Unable to force refresh the access token"); } } } else {