Example usage for twitter4j Twitter getOAuthAccessToken

List of usage examples for twitter4j Twitter getOAuthAccessToken

Introduction

In this page you can find the example usage for twitter4j Twitter getOAuthAccessToken.

Prototype

AccessToken getOAuthAccessToken(RequestToken requestToken, String oauthVerifier) throws TwitterException;

Source Link

Document

Retrieves an access token associated with the supplied request token and sets userId.

Usage

From source file:uk.trainwatch.web.auth.twitter.TwitterCallbackServlet.java

@Override
protected void doSecureGet(ApplicationRequest request) throws ServletException, IOException {
    HttpSession session = request.getRequest().getSession();

    Twitter twitter = (Twitter) session.getAttribute("twitter");
    RequestToken requestToken = (RequestToken) session.getAttribute("requestToken");
    String verifier = request.getParam().get("oauth_verifier");

    try {//from   w w w . ja  v a  2  s .  c  o  m
        if (twitter == null || requestToken == null || verifier == null) {
            request.redirect("/", true);
        } else {
            twitter.getOAuthAccessToken(requestToken, verifier);

            User user = null;
            if (request.isAuthenticated()) {
                // Link to existing user
                user = userFactory.linkTwitter(request.getUser(), twitter);
            } else {
                // Login/Register new user
                user = userFactory.loginOrRegisterTwitter(twitter);
            }

            if (user != null) {
                session.setAttribute(User.KEY, user);
            }

            request.redirect("/home", true);
        }
    } catch (SQLException | TwitterException ex) {
        throw new ServletException(ex);
    } finally {
        session.removeAttribute("twitter");
        session.removeAttribute("requestToken");
    }
}