Search in sources :

Example 1 with AccessToken

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;
}
Also used : AccessToken(com.uwetrottmann.trakt5.entities.AccessToken) IOException(java.io.IOException)

Example 2 with AccessToken

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.");
        }
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) AccessToken(com.uwetrottmann.trakt5.entities.AccessToken) StoredAuthData(nl.dylanvdbrink.flixtraktor.pojo.StoredAuthData) DeviceCode(com.uwetrottmann.trakt5.entities.DeviceCode) IOException(java.io.IOException)

Example 3 with AccessToken

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);
    }
}
Also used : ResponseEntity(net.petafuel.styx.api.exception.ResponseEntity) AccessTokenService(net.petafuel.styx.core.xs2a.standards.ing.v1_0.services.AccessTokenService) AccessToken(net.petafuel.styx.core.xs2a.standards.ing.v1_0.entities.AccessToken) AccessTokenRequest(net.petafuel.styx.core.xs2a.standards.ing.v1_0.http.AccessTokenRequest) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException) StyxException(net.petafuel.styx.api.exception.StyxException)

Aggregations

AccessToken (com.uwetrottmann.trakt5.entities.AccessToken)2 IOException (java.io.IOException)2 DeviceCode (com.uwetrottmann.trakt5.entities.DeviceCode)1 ZonedDateTime (java.time.ZonedDateTime)1 ResponseEntity (net.petafuel.styx.api.exception.ResponseEntity)1 StyxException (net.petafuel.styx.api.exception.StyxException)1 BankRequestFailedException (net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException)1 AccessToken (net.petafuel.styx.core.xs2a.standards.ing.v1_0.entities.AccessToken)1 AccessTokenRequest (net.petafuel.styx.core.xs2a.standards.ing.v1_0.http.AccessTokenRequest)1 AccessTokenService (net.petafuel.styx.core.xs2a.standards.ing.v1_0.services.AccessTokenService)1 StoredAuthData (nl.dylanvdbrink.flixtraktor.pojo.StoredAuthData)1