Example usage for twitter4j Twitter getOAuthRequestToken

List of usage examples for twitter4j Twitter getOAuthRequestToken

Introduction

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

Prototype

RequestToken getOAuthRequestToken(String callbackURL) throws TwitterException;

Source Link

Document

Retrieves a request token

Usage

From source file:Servlet.TwitterAuthServlet.java

License:Apache License

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 *
 * @param request servlet request/* w  w  w  . j  av a 2 s.c o m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    ConfigurationBuilder cb = new ConfigurationBuilder();

    cb.setOAuthConsumerKey(TwitterApp.CONSUMER_KEY);
    cb.setOAuthConsumerSecret(TwitterApp.CONSUMER_SECRET);

    Twitter twitter = new TwitterFactory(cb.build()).getInstance();
    request.getSession().setAttribute("twitter", twitter);
    try {
        StringBuffer callbackURL = request.getRequestURL();
        int index = callbackURL.lastIndexOf("/");
        callbackURL.replace(index, callbackURL.length(), "").append("/callback");

        System.out.println(callbackURL);

        RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());

        System.out.println("Authentication " + requestToken.getAuthenticationURL());
        System.out.println("Authorization " + requestToken.getAuthorizationURL());

        request.getSession().setAttribute("requestToken", requestToken);
        response.sendRedirect(requestToken.getAuthenticationURL());
    } catch (TwitterException e) {
        throw new ServletException(e);
    }
}

From source file:servlets.TwitterConnectionServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    System.out.println("Start TwitterConnectionServlet.doGet");

    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(UtilTwitter.CONSUMER_KEY);
    builder.setOAuthConsumerSecret(UtilTwitter.CONSUMER_SECRET);

    TwitterFactory factory = new TwitterFactory(builder.build());
    Twitter twitter = factory.getInstance();

    RequestToken requestToken;//from   w  w w. j  a  v a  2s  .c om

    try {
        requestToken = twitter.getOAuthRequestToken(UtilTwitter.CALL_BACK_URL);
        String url = requestToken.getAuthenticationURL();

        System.out.println("End TwitterConnectionServlet.doGet");
        response.sendRedirect(url);
    } catch (TwitterException e) {
        e.printStackTrace();
    }
}