use of net.petafuel.styx.core.xs2a.standards.ing.v1_0.entities.AccessToken in project SeriesGuide by UweTrottmann.
the class TraktCredentials method refreshAccessToken.
/**
* Tries to refresh the current access token. Returns {@code false} on failure.
*/
public synchronized boolean refreshAccessToken(TraktV2 trakt) {
// do we even have a refresh token?
String oldRefreshToken = TraktOAuthSettings.getRefreshToken(mContext);
if (TextUtils.isEmpty(oldRefreshToken)) {
Timber.d("refreshAccessToken: no refresh token, give up.");
return false;
}
// try to get a new access token from trakt
String accessToken = null;
String refreshToken = null;
long expiresIn = -1;
try {
Response<AccessToken> response = trakt.refreshAccessToken();
if (response.isSuccessful()) {
AccessToken token = response.body();
accessToken = token.access_token;
refreshToken = token.refresh_token;
expiresIn = token.expires_in;
} else {
if (!SgTrakt.isUnauthorized(response)) {
SgTrakt.trackFailedRequest(mContext, "refresh access token", response);
}
}
} catch (IOException e) {
SgTrakt.trackFailedRequest(mContext, "refresh access token", e);
}
// did we obtain all required data?
if (TextUtils.isEmpty(accessToken) || TextUtils.isEmpty(refreshToken) || expiresIn < 1) {
Timber.e("refreshAccessToken: failed.");
return false;
}
// store the new access token, refresh token and expiry date
if (!setAccessToken(accessToken) || !TraktOAuthSettings.storeRefreshData(mContext, refreshToken, expiresIn)) {
Timber.e("refreshAccessToken: saving failed");
return false;
}
Timber.d("refreshAccessToken: success.");
return true;
}
use of net.petafuel.styx.core.xs2a.standards.ing.v1_0.entities.AccessToken in project flixtraktor by dylanvdbrink.
the class TraktService method ensureAuthorized.
public void ensureAuthorized() throws IOException, TraktException, InterruptedException {
StoredAuthData storedAuthData = storageService.getStoredData().getStoredAuthData();
if (storedAuthData.getTraktAccessToken().isEmpty()) {
log.info("No Trakt access token found, authenticating...");
DeviceCode deviceCode = traktClient.getDeviceCode();
log.info("Authorize the application with the following link: " + deviceCode.verification_url + " and code: " + deviceCode.user_code);
int waitMillis = deviceCode.interval * 1000;
int maxAttempts = deviceCode.expires_in / deviceCode.interval;
boolean userAuthorized = false;
int attempt = 1;
while (!userAuthorized && attempt < maxAttempts) {
log.debug("Attempt #" + attempt);
AccessToken accessToken = traktClient.getToken(deviceCode);
try {
saveAccessToken(accessToken);
userAuthorized = true;
} catch (IOException e) {
Thread.sleep(waitMillis);
attempt++;
}
}
log.info("Application was authorized!");
} else {
log.info("Already have an access code, checking expiration...");
long exp = storedAuthData.getTraktTokenExpiresAt() * 1000;
ZonedDateTime nowZdt = ZonedDateTime.now(ZoneId.of("GMT"));
long now = nowZdt.toInstant().toEpochMilli();
long daysBeforeExp = exp - 1000 * 60 * 60 * 24 * 2;
if (log.isDebugEnabled()) {
ZonedDateTime expZdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(exp), ZoneId.of("GMT"));
ZonedDateTime daysBeforeZdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(daysBeforeExp), ZoneId.of("GMT"));
log.debug("Token expires on: " + expZdt.toString());
log.debug("Current date is: " + nowZdt.toString());
log.debug("2 days before expiration is: " + daysBeforeZdt.toString());
}
if (now > daysBeforeExp) {
log.info("Refreshing access token because token will expire in 2 days.");
AccessToken accessToken = traktClient.refreshAuthorization();
saveAccessToken(accessToken);
} else {
log.info("Access token is still valid.");
}
}
}
use of net.petafuel.styx.core.xs2a.standards.ing.v1_0.entities.AccessToken in project styx by petafuel.
the class STYX09 method generateINGAccessToken.
public void generateINGAccessToken(String url) {
AccessTokenService service = new AccessTokenService();
AccessTokenRequest request = new AccessTokenRequest();
try {
AccessToken retrievedAccessToken = service.tokenRequest(url + "/oauth2/token", request);
// give a tolerance of 30 seconds to the expiry date in case of any software
// related delays
this.accessTokenValidUntil = Instant.now().plusSeconds((retrievedAccessToken.getExpiresIn() - 30));
this.accessToken = retrievedAccessToken;
} catch (BankRequestFailedException e) {
LOG.error("Error getting ing access token:", e);
ResponseEntity responseEntity = new ResponseEntity("Generating ING access token failed", ResponseConstant.INTERNAL_SERVER_ERROR, ResponseCategory.ERROR, ResponseOrigin.STYX);
throw new StyxException(responseEntity);
}
}
Aggregations