mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
Adapt to AccessTokenResponse API change (#13278)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
parent
29fb142e6a
commit
19659b6e05
@ -13,7 +13,7 @@
|
|||||||
package org.openhab.binding.automower.internal.bridge;
|
package org.openhab.binding.automower.internal.bridge;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.LocalDateTime;
|
import java.time.Instant;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
@ -55,7 +55,7 @@ public class AutomowerBridge {
|
|||||||
private AccessTokenResponse authenticate() throws AutomowerCommunicationException {
|
private AccessTokenResponse authenticate() throws AutomowerCommunicationException {
|
||||||
try {
|
try {
|
||||||
AccessTokenResponse result = authService.getAccessTokenResponse();
|
AccessTokenResponse result = authService.getAccessTokenResponse();
|
||||||
if (result == null || result.isExpired(LocalDateTime.now(), 120)) {
|
if (result == null || result.isExpired(Instant.now(), 120)) {
|
||||||
result = authService.getAccessTokenByClientCredentials(null);
|
result = authService.getAccessTokenByClientCredentials(null);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -19,7 +19,7 @@ import java.io.EOFException;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.LocalDateTime;
|
import java.time.Instant;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -151,7 +151,7 @@ public class EcobeeApi implements AccessTokenRefreshListener {
|
|||||||
AccessTokenResponse localAccessTokenResponse = oAuthClientService.getAccessTokenResponse();
|
AccessTokenResponse localAccessTokenResponse = oAuthClientService.getAccessTokenResponse();
|
||||||
if (localAccessTokenResponse != null) {
|
if (localAccessTokenResponse != null) {
|
||||||
logger.trace("API: Got AccessTokenResponse from OAuth service: {}", localAccessTokenResponse);
|
logger.trace("API: Got AccessTokenResponse from OAuth service: {}", localAccessTokenResponse);
|
||||||
if (localAccessTokenResponse.isExpired(LocalDateTime.now(), TOKEN_EXPIRES_IN_BUFFER_SECONDS)) {
|
if (localAccessTokenResponse.isExpired(Instant.now(), TOKEN_EXPIRES_IN_BUFFER_SECONDS)) {
|
||||||
logger.debug("API: Token is expiring soon. Refresh it now");
|
logger.debug("API: Token is expiring soon. Refresh it now");
|
||||||
localAccessTokenResponse = oAuthClientService.refreshToken();
|
localAccessTokenResponse = oAuthClientService.refreshToken();
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,9 @@ import static io.github.bucket4j.Refill.intervally;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -96,8 +98,7 @@ public class HttpHelper {
|
|||||||
try {
|
try {
|
||||||
AccessTokenResponse accessTokenResponse = oAuthClientService.getAccessTokenResponse();
|
AccessTokenResponse accessTokenResponse = oAuthClientService.getAccessTokenResponse();
|
||||||
// refresh the token if it's about to expire
|
// refresh the token if it's about to expire
|
||||||
if (accessTokenResponse != null
|
if (accessTokenResponse != null && accessTokenResponse.isExpired(Instant.now(), OAUTH_EXPIRE_BUFFER)) {
|
||||||
&& accessTokenResponse.isExpired(LocalDateTime.now(), OAUTH_EXPIRE_BUFFER)) {
|
|
||||||
LoggerFactory.getLogger(HttpHelper.class).debug("Requesting a refresh of the access token.");
|
LoggerFactory.getLogger(HttpHelper.class).debug("Requesting a refresh of the access token.");
|
||||||
accessTokenResponse = oAuthClientService.refreshToken();
|
accessTokenResponse = oAuthClientService.refreshToken();
|
||||||
}
|
}
|
||||||
@ -106,11 +107,14 @@ public class HttpHelper {
|
|||||||
String lastToken = lastAccessToken;
|
String lastToken = lastAccessToken;
|
||||||
if (lastToken == null) {
|
if (lastToken == null) {
|
||||||
LoggerFactory.getLogger(HttpHelper.class).debug("The used access token was created at {}",
|
LoggerFactory.getLogger(HttpHelper.class).debug("The used access token was created at {}",
|
||||||
accessTokenResponse.getCreatedOn().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
LocalDateTime.ofInstant(accessTokenResponse.getCreatedOn(), ZoneId.systemDefault())
|
||||||
|
.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
||||||
} else if (!lastToken.equals(accessTokenResponse.getAccessToken())) {
|
} else if (!lastToken.equals(accessTokenResponse.getAccessToken())) {
|
||||||
LoggerFactory.getLogger(HttpHelper.class).debug(
|
LoggerFactory.getLogger(HttpHelper.class)
|
||||||
"The access token changed. New one created at {}",
|
.debug("The access token changed. New one created at {}",
|
||||||
accessTokenResponse.getCreatedOn().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
LocalDateTime
|
||||||
|
.ofInstant(accessTokenResponse.getCreatedOn(), ZoneId.systemDefault())
|
||||||
|
.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
||||||
}
|
}
|
||||||
lastAccessToken = accessTokenResponse.getAccessToken();
|
lastAccessToken = accessTokenResponse.getAccessToken();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user