Example usage for twitter4j.conf Configuration getOAuthAuthorizationURL

List of usage examples for twitter4j.conf Configuration getOAuthAuthorizationURL

Introduction

In this page you can find the example usage for twitter4j.conf Configuration getOAuthAuthorizationURL.

Prototype

String getOAuthAuthorizationURL();

Source Link

Usage

From source file:de.vanita5.twittnuker.util.OAuthPasswordAuthenticator.java

License:Open Source License

public AccessToken getOAuthAccessToken(final String username, final String password)
        throws AuthenticationException {
    if (twitter == null)
        return null;
    final RequestToken requestToken;
    try {/*from w ww.j  a v  a 2s .  co m*/
        requestToken = twitter.getOAuthRequestToken(OAUTH_CALLBACK_OOB);
    } catch (final TwitterException e) {
        if (e.isCausedByNetworkIssue())
            throw new AuthenticationException(e);
        throw new AuthenticityTokenException();
    }
    try {
        final String oauthToken = requestToken.getToken();
        final String authorizationUrl = requestToken.getAuthorizationURL().toString();
        final String authenticityToken = readAuthenticityTokenFromHtml(
                client.get(authorizationUrl, authorizationUrl, null, null).asReader());
        if (authenticityToken == null)
            throw new AuthenticityTokenException();
        final Configuration conf = twitter.getConfiguration();
        final HttpParameter[] params = new HttpParameter[4];
        params[0] = new HttpParameter("authenticity_token", authenticityToken);
        params[1] = new HttpParameter("oauth_token", oauthToken);
        params[2] = new HttpParameter("session[username_or_email]", username);
        params[3] = new HttpParameter("session[password]", password);
        final String oAuthAuthorizationUrl = conf.getOAuthAuthorizationURL().toString();
        final String oauthPin = readOAuthPINFromHtml(
                client.post(oAuthAuthorizationUrl, oAuthAuthorizationUrl, params).asReader());
        if (isEmpty(oauthPin))
            throw new WrongUserPassException();
        return twitter.getOAuthAccessToken(requestToken, oauthPin);
    } catch (final IOException e) {
        throw new AuthenticationException(e);
    } catch (final TwitterException e) {
        throw new AuthenticationException(e);
    } catch (final NullPointerException e) {
        throw new AuthenticationException(e);
    } catch (final XmlPullParserException e) {
        throw new AuthenticationException(e);
    }
}

From source file:org.getlantern.firetweet.util.OAuthPasswordAuthenticator.java

License:Open Source License

public AccessToken getOAuthAccessToken(final String username, final String password)
        throws AuthenticationException {
    final RequestToken requestToken;
    try {/* w  w w  .j  av a  2s .c om*/
        requestToken = twitter.getOAuthRequestToken(OAUTH_CALLBACK_OOB);
    } catch (final TwitterException e) {
        if (e.isCausedByNetworkIssue())
            throw new AuthenticationException(e);
        throw new AuthenticityTokenException();
    }
    try {
        final String oauthToken = requestToken.getToken();
        final String authorizationUrl = requestToken.getAuthorizationURL();
        final HashMap<String, String> inputMap = new HashMap<>();
        final HttpResponse authorizePage = client.get(authorizationUrl, authorizationUrl, null, null, null);
        final List<String> cookieHeaders = authorizePage.getResponseHeaders("Set-Cookie");
        readInputFromHtml(authorizePage.asReader(), inputMap, INPUT_AUTHENTICITY_TOKEN,
                INPUT_REDIRECT_AFTER_LOGIN);
        final Configuration conf = twitter.getConfiguration();
        final List<HttpParameter> params = new ArrayList<>();
        params.add(new HttpParameter("oauth_token", oauthToken));
        params.add(new HttpParameter(INPUT_AUTHENTICITY_TOKEN, inputMap.get(INPUT_AUTHENTICITY_TOKEN)));
        if (inputMap.containsKey(INPUT_REDIRECT_AFTER_LOGIN)) {
            params.add(new HttpParameter(INPUT_REDIRECT_AFTER_LOGIN, inputMap.get(INPUT_REDIRECT_AFTER_LOGIN)));
        }
        params.add(new HttpParameter("session[username_or_email]", username));
        params.add(new HttpParameter("session[password]", password));
        final HeaderMap requestHeaders = new HeaderMap();
        requestHeaders.addHeader("Origin", "https://twitter.com");
        requestHeaders.addHeader("Referer",
                "https://twitter.com/oauth/authorize?oauth_token=" + requestToken.getToken());
        requestHeaders.put("Cookie", cookieHeaders);
        final String oAuthAuthorizationUrl = conf.getOAuthAuthorizationURL();
        final String oauthPin = readOAuthPINFromHtml(client.post(oAuthAuthorizationUrl, oAuthAuthorizationUrl,
                params.toArray(new HttpParameter[params.size()]), requestHeaders).asReader());
        if (isEmpty(oauthPin))
            throw new WrongUserPassException();
        return twitter.getOAuthAccessToken(requestToken, oauthPin);
    } catch (final IOException | TwitterException | NullPointerException | XmlPullParserException e) {
        throw new AuthenticationException(e);
    }
}

From source file:org.mariotaku.twidere.util.OAuthPasswordAuthenticator.java

License:Open Source License

public AccessToken getOAuthAccessToken(final String username, final String password)
        throws AuthenticationException {
    final RequestToken requestToken;
    try {//from w w  w  .j  av a2  s  .c  om
        requestToken = twitter.getOAuthRequestToken(OAUTH_CALLBACK_OOB);
    } catch (final TwitterException e) {
        if (e.isCausedByNetworkIssue())
            throw new AuthenticationException(e);
        throw new AuthenticityTokenException();
    }
    HttpResponse authorizePage = null, authorizeResult = null;
    try {
        final String oauthToken = requestToken.getToken();
        final String authorizationUrl = requestToken.getAuthorizationURL();
        final HashMap<String, String> inputMap = new HashMap<>();
        authorizePage = client.get(authorizationUrl, authorizationUrl, null, null, null);
        final List<String> cookieHeaders = authorizePage.getResponseHeaders("Set-Cookie");
        readInputFromHtml(authorizePage.asReader(), inputMap, INPUT_AUTHENTICITY_TOKEN,
                INPUT_REDIRECT_AFTER_LOGIN);
        final Configuration conf = twitter.getConfiguration();
        final List<HttpParameter> params = new ArrayList<>();
        params.add(new HttpParameter("oauth_token", oauthToken));
        params.add(new HttpParameter(INPUT_AUTHENTICITY_TOKEN, inputMap.get(INPUT_AUTHENTICITY_TOKEN)));
        if (inputMap.containsKey(INPUT_REDIRECT_AFTER_LOGIN)) {
            params.add(new HttpParameter(INPUT_REDIRECT_AFTER_LOGIN, inputMap.get(INPUT_REDIRECT_AFTER_LOGIN)));
        }
        params.add(new HttpParameter("session[username_or_email]", username));
        params.add(new HttpParameter("session[password]", password));
        final HeaderMap requestHeaders = new HeaderMap();
        requestHeaders.addHeader("Origin", "https://twitter.com");
        requestHeaders.addHeader("Referer",
                "https://twitter.com/oauth/authorize?oauth_token=" + requestToken.getToken());
        final List<String> modifiedCookieHeaders = new ArrayList<>();
        final String oAuthAuthorizationUrl = conf.getOAuthAuthorizationURL();

        final String host = parseUrlHost(oAuthAuthorizationUrl);
        for (String cookieHeader : cookieHeaders) {
            for (HttpCookie cookie : HttpCookie.parse(cookieHeader)) {
                if (HttpCookie.domainMatches(cookie.getDomain(), host)) {
                    cookie.setVersion(1);
                    cookie.setDomain("twitter.com");
                }
                modifiedCookieHeaders.add(cookie.toString());
            }
        }
        requestHeaders.put("Cookie", modifiedCookieHeaders);
        authorizeResult = client.post(oAuthAuthorizationUrl, oAuthAuthorizationUrl,
                params.toArray(new HttpParameter[params.size()]), requestHeaders);
        final String oauthPin = readOAuthPINFromHtml(authorizeResult.asReader());
        if (isEmpty(oauthPin))
            throw new WrongUserPassException();
        return twitter.getOAuthAccessToken(requestToken, oauthPin);
    } catch (final IOException | TwitterException | NullPointerException | XmlPullParserException e) {
        throw new AuthenticationException(e);
    } finally {
        if (authorizePage != null) {
            try {
                authorizePage.disconnect();
            } catch (IOException ignore) {
            }
        }
        if (authorizeResult != null) {
            try {
                authorizeResult.disconnect();
            } catch (IOException ignore) {
            }
        }
    }
}