Search in sources :

Example 1 with AccessToken

use of com.uwetrottmann.trakt5.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)

Aggregations

AccessToken (com.uwetrottmann.trakt5.entities.AccessToken)1 IOException (java.io.IOException)1