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:com.mycompany.twittersearch.SignIn.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   w w w .  j av  a 2s.c o m*/
 *
 * @param request servlet request
 * @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 {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("").setOAuthConsumerSecret("");

    Twitter twitter = new TwitterFactory(cb.build()).getInstance();

    request.getSession().setAttribute("twitter", twitter);
    try {
        StringBuffer callbackURL = request.getRequestURL();
        System.out.println("TwitterLoginServlet:callbackURL:" + callbackURL);

        int index = callbackURL.lastIndexOf("/");
        callbackURL.replace(index, callbackURL.length(), "").append("/Callback");

        RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());
        request.getSession().setAttribute("requestToken", requestToken);
        System.out.println("requestToken.getAuthenticationURL():" + requestToken.getAuthenticationURL());
        response.sendRedirect(requestToken.getAuthenticationURL());

    } catch (TwitterException e) {
        throw new ServletException(e);
    }

}

From source file:com.practicando.controllers.adapters.LoginTwitterAdapter.java

@Override
public String login(HttpServletRequest request) throws TwitterException {
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer("t2UVo2VHdoQx4QoP9GdysJzoA", "9D7wrEtAr2ffPJisbd68iUPQZohp0KW6Xh3fIUhPs2RGq3K477");
    request.getSession().setAttribute("twitter", twitter);
    StringBuffer callbackURL = request.getRequestURL();
    int index = callbackURL.lastIndexOf("/");
    callbackURL.replace(index, callbackURL.length(), "").append("/login-twitter-callback");
    RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());
    request.getSession().setAttribute("requestToken", requestToken);
    return requestToken.getAuthenticationURL();

}

From source file:com.tuncaysenturk.jira.plugins.compatibility.servlet.TwitterLoginServlet.java

public void login(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> context) {
    Twitter twitter = new TwitterFactory().getInstance();

    try {// w  ww.ja va  2s  . c  om
        PropertySet propSet = ComponentManager.getComponent(PropertiesManager.class).getPropertySet();
        URI servletConfigureTwitter = URI
                .create(applicationProperties.getBaseUrl() + JTPConstants.URL_CONFIGURE_TWITTER);
        String callbackURL = servletConfigureTwitter + QUESTION_MARK + IS_CALLBACK + EQUALS + TRUE;
        twitter.setOAuthConsumer(propSet.getString("consumerKey"), propSet.getString("consumerSecret"));
        RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL);

        String requestToken_token = requestToken.getToken();
        String requestToken_secret = requestToken.getTokenSecret();

        propSet.setString("requestToken", requestToken_token);
        propSet.setString("requestTokenSecret", requestToken_secret);

        String redirectUrl = requestToken.getAuthorizationURL();
        resp.sendRedirect(redirectUrl);

    } catch (TwitterException e) {
        logger.error(JTPConstants.LOG_PRE + "Exception while logging to Twitter", e);
        @SuppressWarnings("unchecked")
        List<String> errorMessages = (List<String>) context.get("errorMessages");
        errorMessages.add(e.getMessage());
    } catch (IOException e) {
        logger.error(JTPConstants.LOG_PRE + "Exception while logging to Twitter", e);
    }
}

From source file:com.twitstreet.servlet.SigninServlet.java

License:Open Source License

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
    response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
    response.setHeader("Pragma", "no-cache"); //HTTP 1.0
    response.setDateHeader("Expires", 0); //prevents caching at the proxy server

    Twitter twitter = new TwitterFactory().getInstance();
    try {//from www. ja v a  2s. c  o m
        StringBuffer callbackURL = request.getRequestURL();
        int index = callbackURL.lastIndexOf("/");
        callbackURL.replace(index, callbackURL.length(), "").append("/callback");
        logger.debug("Callback url is: " + callbackURL.toString());
        twitter.setOAuthConsumer(configMgr.getConsumerKey(), configMgr.getConsumerSecret());
        logger.debug("Consumer Key: " + configMgr.getConsumerKey() + ", Consumer Secret: "
                + configMgr.getConsumerSecret());
        RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());
        request.getSession().setAttribute(CallBackServlet.REQUEST_TOKEN, requestToken);
        response.sendRedirect(requestToken.getAuthenticationURL());
        logger.debug("Redirect sent to authentication URL: " + requestToken.getAuthenticationURL());

    } catch (TwitterException e) {
        throw new ServletException(e);
    }
}

From source file:edu.harvard.iq.dvn.core.web.admin.OptionsPage.java

public String authorizeTwitter() {
    String callbackURL = "http://" + PropertyUtil.getHostUrl() + "/dvn";
    callbackURL += getVDCRequestBean().getCurrentVDC() == null ? "/faces/networkAdmin/NetworkOptionsPage.xhtml"
            : getVDCRequestBean().getCurrentVDCURL() + "/faces/admin/OptionsPage.xhtml";

    Twitter twitter = new TwitterFactory().getInstance();

    try {/*from w  w  w  .j  a  v a  2s.c  o m*/
        RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL);
        getSessionMap().put("requestToken", requestToken);
        redirect(requestToken.getAuthorizationURL());
    } catch (TwitterException te) {
        te.printStackTrace();
    }

    return null;
}

From source file:edu.stanford.muse.slant.SigninServlet.java

License:Apache License

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {//from  w  w  w.  j a  v a  2  s.  c om
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(OAuth_consumerKey, OAuth_consumerSecret);
        request.getSession().setAttribute("twitter", twitter);
        StringBuffer callbackURL = request.getRequestURL();
        int index = callbackURL.lastIndexOf("/");
        callbackURL.replace(index, callbackURL.length(), "").append("/callback");

        RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());
        request.getSession().setAttribute("requestToken", requestToken);
        response.sendRedirect(requestToken.getAuthenticationURL());

    } catch (Exception e) {
        System.out.println(e);
    }

}

From source file:ens.demo.twitter.SigninServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w w w  .  jav a2 s . c o m
 *
 * @param request servlet request
 * @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 {
    Twitter twitter = new TwitterFactory().getInstance();
    request.getSession().setAttribute("twitter", twitter);
    try {
        StringBuffer callbackURL = request.getRequestURL();
        int index = callbackURL.lastIndexOf("/");
        callbackURL.replace(index, callbackURL.length(), "").append("/callback");

        RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());
        request.getSession().setAttribute("requestToken", requestToken);
        response.sendRedirect(requestToken.getAuthenticationURL());

    } catch (TwitterException e) {
        throw new ServletException(e);
    }
}

From source file:h2weibo.controllers.AuthServlet.java

License:Open Source License

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpServletRouter r = new HttpServletRouter(request);
    r.setPattern("/:type");

    response.setContentType("text/plain");
    PrintWriter writer = response.getWriter();
    String serverPath = "http://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath();/*from w  ww.  j  a v  a2s  . c  o m*/

    HttpSession session = request.getSession();

    if (r.is(":type", "weibo")) {
        try {
            Weibo weibo = new Weibo();
            RequestToken requestToken = weibo.getOAuthRequestToken(serverPath + "/callback/weibo");

            response.setStatus(302);
            response.setHeader("Location", requestToken.getAuthenticationURL());
            session.setAttribute(Keys.SESSION_TOKEN, requestToken.getToken());
            session.setAttribute(Keys.SESSION_TOKEN_SECRET, requestToken.getTokenSecret());

            log.info("Redirecting Weibo...");
        } catch (WeiboException e) {
            log.error(e);
        }
    } else if (r.is(":type", "twitter")) {
        try {
            TwitterFactory factory = new TwitterFactory();
            Twitter t = factory.getInstance();
            twitter4j.auth.RequestToken requestToken = t.getOAuthRequestToken(serverPath + "/callback/twitter");

            response.setStatus(302);
            response.setHeader("Location", requestToken.getAuthenticationURL());
            session.setAttribute(Keys.SESSION_REQUEST_TOKEN, requestToken);

            log.info("Redirecting Twitter...");
        } catch (TwitterException e) {
            log.error(e);
        }
        writer.close();
    } else {
        response.setStatus(200);
        writer.println("Wrong parameter, not working!");
        writer.close();
    }
}

From source file:jp.wda.azeda.core.logics.impl.TwitterServiceImpl.java

License:Apache License

/** {@inheritDoc} */
@Override/*  w w  w .j a  v  a 2s  .  c o m*/
public CheckAuthorizedResult checkAuthorized(String account, String callbackURL) throws TwitterException {
    CheckAuthorizedResult result = new CheckAuthorizedResult();
    StoredAccessToken at = accessTokensDao.getAccessToken(account);
    if (at != null) {
        return result;
    }

    Twitter twitter = new TwitterFactory().getInstance();
    String requestID = UUID.randomUUID().toString();
    RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL + requestID);
    requestTokens.put(requestID, requestToken);

    result.status = -1;
    result.redirectTo = requestToken.getAuthorizationURL();

    return result;
}

From source file:net.awairo.favdler.oauth.TwitterOAuth.java

License:MIT License

private MyTask.Function<RequestToken> getRequestToken(Twitter t) {
    return () -> {
        try {//from w w w  . j a va  2 s. co  m
            return t.getOAuthRequestToken(String.format(CALLBACK, server.getLocalPort()));
        } catch (TwitterException e) {
            throw new AppException(e);
        }
    };
}