Search in sources :

Example 1 with OAuthData

use of net.dean.jraw.http.oauth.OAuthData in project Slide by ccrama.

the class Authentication method doVerify.

public static void doVerify(String lastToken, RedditClient baseReddit, boolean single, Context mContext) {
    try {
        String token = lastToken;
        if (BuildConfig.DEBUG)
            LogUtil.v("TOKEN IS " + token);
        if (!token.isEmpty()) {
            Credentials credentials = Credentials.installedApp(CLIENT_ID, REDIRECT_URL);
            OAuthHelper oAuthHelper = baseReddit.getOAuthHelper();
            oAuthHelper.setRefreshToken(token);
            try {
                OAuthData finalData;
                if (!single && authentication.contains("backedCreds") && authentication.getLong("expires", 0) > Calendar.getInstance().getTimeInMillis()) {
                    finalData = oAuthHelper.refreshToken(credentials, authentication.getString("backedCreds", ""));
                } else {
                    // does a request
                    finalData = oAuthHelper.refreshToken(credentials);
                    if (!single) {
                        authentication.edit().putLong("expires", Calendar.getInstance().getTimeInMillis() + 3000000).apply();
                    }
                }
                baseReddit.authenticate(finalData);
                if (!single) {
                    authentication.edit().putString("backedCreds", finalData.getDataNode().toString()).apply();
                    refresh = oAuthHelper.getRefreshToken();
                    if (BuildConfig.DEBUG) {
                        LogUtil.v("ACCESS TOKEN IS " + finalData.getAccessToken());
                    }
                    Authentication.isLoggedIn = true;
                    UserSubscriptions.doCachedModSubs();
                }
            } catch (Exception e) {
                e.printStackTrace();
                if (e instanceof NetworkException) {
                    Toast.makeText(mContext, "Error " + ((NetworkException) e).getResponse().getStatusMessage() + ": " + (e).getMessage(), Toast.LENGTH_LONG).show();
                }
            }
            didOnline = true;
        } else if (!single) {
            if (BuildConfig.DEBUG)
                LogUtil.v("NOT LOGGED IN");
            final Credentials fcreds = Credentials.userlessApp(CLIENT_ID, UUID.randomUUID());
            OAuthData authData;
            try {
                authData = reddit.getOAuthHelper().easyAuth(fcreds);
                authentication.edit().putLong("expires", Calendar.getInstance().getTimeInMillis() + 3000000).apply();
                authentication.edit().putString("backedCreds", authData.getDataNode().toString()).apply();
                reddit.authenticate(authData);
                Authentication.name = "LOGGEDOUT";
                Reddit.notFirst = true;
                didOnline = true;
            } catch (Exception e) {
                e.printStackTrace();
                if (e instanceof NetworkException) {
                    Toast.makeText(mContext, "Error " + ((NetworkException) e).getResponse().getStatusMessage() + ": " + (e).getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }
        if (!single)
            authedOnce = true;
    } catch (Exception e) {
    // TODO fail
    }
}
Also used : OAuthHelper(net.dean.jraw.http.oauth.OAuthHelper) NetworkException(net.dean.jraw.http.NetworkException) Credentials(net.dean.jraw.http.oauth.Credentials) OAuthData(net.dean.jraw.http.oauth.OAuthData) NetworkException(net.dean.jraw.http.NetworkException)

Aggregations

NetworkException (net.dean.jraw.http.NetworkException)1 Credentials (net.dean.jraw.http.oauth.Credentials)1 OAuthData (net.dean.jraw.http.oauth.OAuthData)1 OAuthHelper (net.dean.jraw.http.oauth.OAuthHelper)1