Skip to content

Commit 8644a77

Browse files
authored
Improve handling of error code 14 (openhab#16613)
Signed-off-by: Mark Hilbush <[email protected]>
1 parent bf486e3 commit 8644a77

File tree

1 file changed

+14
-9
lines changed
  • bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api

1 file changed

+14
-9
lines changed

bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public class EcobeeApi {
8383
private static final int ECOBEE_TOKEN_EXPIRED = 14;
8484
private static final int ECOBEE_DEAUTHORIZED_TOKEN = 16;
8585
private static final int TOKEN_EXPIRES_IN_BUFFER_SECONDS = 120;
86+
private static final boolean FORCE_TOKEN_REFRESH = true;
87+
private static final boolean DONT_FORCE_TOKEN_REFRESH = false;
8688

8789
public static final Properties HTTP_HEADERS;
8890
static {
@@ -147,14 +149,15 @@ public void closeOAuthClientService() {
147149
* response, then assume that the Ecobee authorization process is complete. Otherwise,
148150
* start the Ecobee authorization process.
149151
*/
150-
private boolean isAuthorized() {
152+
private boolean isAuthorized(boolean forceTokenRefresh) {
151153
boolean isAuthorized = false;
152154
try {
153155
AccessTokenResponse localAccessTokenResponse = oAuthClientService.getAccessTokenResponse();
154156
if (localAccessTokenResponse != null) {
155157
logger.trace("API: Got AccessTokenResponse from OAuth service: {}", localAccessTokenResponse);
156-
if (localAccessTokenResponse.isExpired(Instant.now(), TOKEN_EXPIRES_IN_BUFFER_SECONDS)) {
157-
logger.debug("API: Token is expiring soon. Refresh it now");
158+
if (forceTokenRefresh
159+
|| localAccessTokenResponse.isExpired(Instant.now(), TOKEN_EXPIRES_IN_BUFFER_SECONDS)) {
160+
logger.debug("API: Refreshing access token");
158161
localAccessTokenResponse = oAuthClientService.refreshToken();
159162
}
160163
ecobeeAuth.setState(EcobeeAuthState.COMPLETE);
@@ -187,6 +190,10 @@ private boolean isAuthorized() {
187190
return isAuthorized;
188191
}
189192

193+
private boolean isAuthorized() {
194+
return isAuthorized(DONT_FORCE_TOKEN_REFRESH);
195+
}
196+
190197
private void handleOAuthException(OAuthResponseException e) {
191198
if ("invalid_grant".equalsIgnoreCase(e.getError())) {
192199
// Usually indicates that the refresh token is no longer valid and will require reauthorization
@@ -346,15 +353,13 @@ private boolean isSuccess(@Nullable AbstractResponseDTO response) {
346353
logger.debug("API: AccessTokenResponse created on: {}", localAccessTokenResponse.getCreatedOn());
347354
logger.debug("API: AccessTokenResponse expires in: {}", localAccessTokenResponse.getExpiresIn());
348355
}
349-
// Recreating the OAuthClientService seems to be the only way to handle this error
350-
closeOAuthClientService();
351-
createOAuthClientService();
352-
if (isAuthorized()) {
356+
logger.debug("API: Ecobee API attempting to force an access token refresh");
357+
if (isAuthorized(FORCE_TOKEN_REFRESH)) {
353358
return true;
354359
} else {
355-
logger.warn("API: isAuthorized was NOT successful on second try");
360+
logger.warn("API: isAuthorized was NOT successful forcing the access token refresh");
356361
bridgeHandler.updateBridgeStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
357-
"Unable to refresh access token");
362+
"Unable to force refresh the access token");
358363
}
359364
}
360365
} else {

0 commit comments

Comments
 (0)