Skip to content

Commit

Permalink
Improve handling of error code 14 (openhab#16613)
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Hilbush <[email protected]>
  • Loading branch information
mhilbush authored Apr 5, 2024
1 parent bf486e3 commit 8644a77
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -147,14 +149,15 @@ public void closeOAuthClientService() {
* 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);
Expand Down Expand Up @@ -187,6 +190,10 @@ private boolean isAuthorized() {
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
Expand Down Expand Up @@ -346,15 +353,13 @@ private boolean isSuccess(@Nullable AbstractResponseDTO response) {
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 {
Expand Down

0 comments on commit 8644a77

Please sign in to comment.