@@ -83,6 +83,8 @@ public class EcobeeApi {
83
83
private static final int ECOBEE_TOKEN_EXPIRED = 14 ;
84
84
private static final int ECOBEE_DEAUTHORIZED_TOKEN = 16 ;
85
85
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 ;
86
88
87
89
public static final Properties HTTP_HEADERS ;
88
90
static {
@@ -147,14 +149,15 @@ public void closeOAuthClientService() {
147
149
* response, then assume that the Ecobee authorization process is complete. Otherwise,
148
150
* start the Ecobee authorization process.
149
151
*/
150
- private boolean isAuthorized () {
152
+ private boolean isAuthorized (boolean forceTokenRefresh ) {
151
153
boolean isAuthorized = false ;
152
154
try {
153
155
AccessTokenResponse localAccessTokenResponse = oAuthClientService .getAccessTokenResponse ();
154
156
if (localAccessTokenResponse != null ) {
155
157
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" );
158
161
localAccessTokenResponse = oAuthClientService .refreshToken ();
159
162
}
160
163
ecobeeAuth .setState (EcobeeAuthState .COMPLETE );
@@ -187,6 +190,10 @@ private boolean isAuthorized() {
187
190
return isAuthorized ;
188
191
}
189
192
193
+ private boolean isAuthorized () {
194
+ return isAuthorized (DONT_FORCE_TOKEN_REFRESH );
195
+ }
196
+
190
197
private void handleOAuthException (OAuthResponseException e ) {
191
198
if ("invalid_grant" .equalsIgnoreCase (e .getError ())) {
192
199
// Usually indicates that the refresh token is no longer valid and will require reauthorization
@@ -346,15 +353,13 @@ private boolean isSuccess(@Nullable AbstractResponseDTO response) {
346
353
logger .debug ("API: AccessTokenResponse created on: {}" , localAccessTokenResponse .getCreatedOn ());
347
354
logger .debug ("API: AccessTokenResponse expires in: {}" , localAccessTokenResponse .getExpiresIn ());
348
355
}
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 )) {
353
358
return true ;
354
359
} 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 " );
356
361
bridgeHandler .updateBridgeStatus (ThingStatus .OFFLINE , ThingStatusDetail .COMMUNICATION_ERROR ,
357
- "Unable to refresh access token" );
362
+ "Unable to force refresh the access token" );
358
363
}
359
364
}
360
365
} else {
0 commit comments