List of usage examples for twitter4j Twitter getOAuthRequestToken
RequestToken getOAuthRequestToken(String callbackURL) throws TwitterException;
From source file:ar.com.zauber.commons.social.oauth.twitter.Twitter4JOAuthAccessManager.java
License:Apache License
/** @return the request token */ private RequestToken getRequestToken(final String callbackUrl) { try {/* w w w.j a v a2 s . c o m*/ Twitter twitter = twitterFactory.getOAuthAuthorizedInstance(consumer.getKey(), consumer.getSecret()); if (StringUtils.isNotEmpty(callbackUrl)) { return twitter.getOAuthRequestToken(callbackUrl); } else { return twitter.getOAuthRequestToken(); } } catch (TwitterException e) { throw new OAuthAccessException("Exception when getting request token", e); } }
From source file:bluevia.OAuth.java
License:Apache License
private String oAuthTwitter(HttpServletRequest req, String step) { String oAuthUrl = "/index.jsp"; Properties prop = new Properties(); prop.setProperty("oauth.consumerKey", TwitterOAuth.consumer_key); prop.setProperty("oauth.consumerSecret", TwitterOAuth.consumer_secret); Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(TwitterOAuth.consumer_key, TwitterOAuth.consumer_secret); try {//from w ww .j a v a 2s. co m if (step.compareTo("1") == 0) { RequestToken requestToken = twitter .getOAuthRequestToken(Util.getCallbackDomain() + "/oauth?network=twitter&step=2"); req.getSession().setAttribute("request_key", requestToken.getToken()); req.getSession().setAttribute("request_secret", requestToken.getTokenSecret()); oAuthUrl = requestToken.getAuthenticationURL(); } else { String request_key = (String) req.getSession().getAttribute("request_key"); String request_secret = (String) req.getSession().getAttribute("request_secret"); String oauth_verifier = req.getParameter("oauth_verifier"); RequestToken requestToken = new RequestToken(request_key, request_secret); AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, oauth_verifier); UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); Util.addNetworkAccount(user.getEmail(), "TwitterAccount", Util.TwitterOAuth.consumer_key, Util.TwitterOAuth.consumer_secret, accessToken.getToken(), accessToken.getTokenSecret()); req.getSession().removeAttribute("request_key"); req.getSession().removeAttribute("request_secret"); oAuthUrl = "/settings.jsp"; } } catch (TwitterException e) { // TODO Auto-generated catch block e.printStackTrace(); logger.severe(e.getMessage()); } catch (Exception e) { e.printStackTrace(); logger.severe(e.getMessage()); } return oAuthUrl; }
From source file:com.anshul.LoginServlet.java
License:Open Source License
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { try {/*from w w w. j av a2 s. com*/ Twitter twitter = new TwitterFactory().getInstance(); try { twitter.setOAuthConsumer("dyHSJLTZhh2eSSw2RVpqMIKyJ", "jW22uPPA54DOMNicfFJWzf4jB7nKHbdi7L6VuPFlfWEPAOt5Ai"); } catch (IllegalStateException e) { e.printStackTrace(); } StringBuffer callbackURL = req.getRequestURL(); int index = callbackURL.lastIndexOf("/"); callbackURL.replace(index, callbackURL.length(), "").append("/home"); RequestToken requestToken; requestToken = twitter.getOAuthRequestToken(callbackURL.toString()); String token = requestToken.getToken(); String tokenSecret = requestToken.getTokenSecret(); HttpSession session = req.getSession(); session.setAttribute("twitter", twitter); String authUrl = requestToken.getAuthorizationURL(); // session.setAttribute("authUrl", authUrl); req.setAttribute("authUrl", authUrl); RequestDispatcher rd = req.getRequestDispatcher("index.jsp"); rd.forward(req, resp); } catch (TwitterException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ServletException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.appspot.bitlyminous.command.authentication.AbstractAuthenticationCommand.java
License:Apache License
/** * Execute twitter o auth.//from www . j av a2 s . c om * * @param req the req * @param resp the resp * @param callbackUrl the callback url * * @return true, if successful * * @throws Exception the exception */ protected boolean executeTwitterOAuth(HttpServletRequest req, HttpServletResponse resp, String callbackUrl) throws Exception { HttpSession session = req.getSession(true); Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(ApplicationConstants.TWITTER_CONSUMER_KEY, ApplicationConstants.TWITTER_CONSUMER_SECRET); if (session.getAttribute(ParameterNames.REQUEST_TOKEN_PARAM) == null || session.getAttribute(ParameterNames.REQUEST_TOKEN_SECRET_PARAM) == null) { RequestToken requestToken = twitter.getOAuthRequestToken(callbackUrl); session.setAttribute(ParameterNames.REQUEST_TOKEN_PARAM, requestToken.getToken()); session.setAttribute(ParameterNames.REQUEST_TOKEN_SECRET_PARAM, requestToken.getTokenSecret()); resp.sendRedirect(requestToken.getAuthenticationURL()); return false; } else { AccessToken accessToken = twitter.getOAuthAccessToken( new RequestToken((String) session.getAttribute(ParameterNames.REQUEST_TOKEN_PARAM), (String) session.getAttribute(ParameterNames.REQUEST_TOKEN_SECRET_PARAM)), req.getParameter(ParameterNames.OAUTH_VERIFIER)); session.removeAttribute(ParameterNames.REQUEST_TOKEN_PARAM); session.removeAttribute(ParameterNames.REQUEST_TOKEN_SECRET_PARAM); User user = userService.getUserByScreenName(accessToken.getScreenName()); if (user == null) { user = new User(); user.setScreenName(accessToken.getScreenName()); } user.setTwitterToken(new Text(accessToken.getToken())); user.setTwitterSecret(new Text(accessToken.getTokenSecret())); session.setAttribute(ParameterNames.SESSION_USER, user); return true; } }
From source file:com.djbrick.twitter_photo_uploader.MSTwitterService.java
License:Apache License
/** * Get the authorization URL and send it back in a sticky broadcast. *//*from www .jav a 2s .com*/ private void processGetAuthURL() { //create a RequestToken to use to create the request URL // token will be used later to decode result from twitter.com // and needs to be saved to static variable in MSTwitter RequestToken reqToken = null; Twitter twitter4j = null; String url = null; int resultCode = MSTwitter.MST_RESULT_SUCCESSFUL; // be optimistic try { twitter4j = new TwitterFactory().getInstance(); twitter4j.setOAuthConsumer(MSTwitter.smConsumerKey, MSTwitter.smConsumerSecret); } catch (IllegalStateException e) { // No network access or token already available resultCode = MSTwitter.MST_RESULT_ILLEGAL_STATE_SETOAUTHCONSUMER; Log.e(MSTwitter.TAG, e.toString()); } // get the token if (resultCode == MSTwitter.MST_RESULT_SUCCESSFUL) { try { reqToken = twitter4j.getOAuthRequestToken(MSTwitter.CALLBACK_URL); } catch (TwitterException e) { int tErrorNum = MSTwitter.getTwitterErrorNum(e, this); // No network access resultCode = tErrorNum; Log.e(MSTwitter.TAG, e.getExceptionCode() + ": " + e.getMessage()); } catch (IllegalStateException e) { // No network access or token already available resultCode = MSTwitter.MST_RESULT_ILLEGAL_STATE_TOKEN_ALREADY_AVALIABLE; Log.e(MSTwitter.TAG, e.toString()); } } // if we got the request token then use it to get the url if (resultCode == MSTwitter.MST_RESULT_SUCCESSFUL) { url = reqToken.getAuthenticationURL(); // save the request token MSTwitter.smReqToken = reqToken; } // broadcast the results Intent broadcastIntent = new Intent(); broadcastIntent.setAction(MSTwitter.INTENT_BROADCAST_MSTWITTER); broadcastIntent.putExtra(MSTwitterService.MST_KEY_SERVICE_TASK, MSTwitterService.MST_SERVICE_TASK_GET_AUTH_URL); broadcastIntent.putExtra(MSTwitterService.MST_KEY_AUTHURL_RESULT, resultCode); if (url != null) { broadcastIntent.putExtra(MSTwitterService.MST_KEY_AUTHURL_RESULT_URL, url); } broadcastIntent.putExtra(MSTwitterService.MST_KEY_TWEET_TEXT, mText); broadcastIntent.putExtra(MSTwitterService.MST_KEY_TWEET_IMAGE_PATH, mImagePath); sendStickyBroadcast(broadcastIntent); }
From source file:com.metadot.book.connectr.server.servlets.LoginTwitterServlet.java
License:Apache License
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { Twitter twitter = new TwitterFactory().getInstance(); // get auth info from system properties String key = AuthenticationProvider.getProp("twitter-consumer-key"); String secret = AuthenticationProvider.getProp("twitter-consumer-secret"); if (key == null || secret == null) { response.setContentType("text/html"); response.getWriter().print(AppLib.INFONOTFOUND); return;/*from w ww . ja v a 2 s . co m*/ } try { twitter.setOAuthConsumer(key, secret); String callbackURL = buildCallBackURL(request, AuthenticationProvider.TWITTER); RequestToken token = twitter.getOAuthRequestToken(callbackURL); request.getSession().setAttribute("requestToken", token); String loginURL = token.getAuthenticationURL() + "&force_login=true"; log.info("Redirecting to: " + loginURL); response.sendRedirect(loginURL); } catch (TwitterException e) { response.setContentType("text/html"); response.getWriter().print("<p>" + e.getMessage() + "</p>"); response.getWriter().print(AppLib.INFONOTFOUND); return; // e.printStackTrace(); } }
From source file:com.michaelstark.twitteroauth.LoginServlet.java
/** * Handles the HTTP <code>POST</code> method. * Begins the OAuth authentication mechanism. * * @param request servlet request/*from w w w .j av a 2s .c o m*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setOAuthConsumerKey("<consumer key goes here>") .setOAuthConsumerSecret("<consumer secret goes here>"); Twitter twitter = new TwitterFactory(builder.build()).getInstance(); HttpSession session = request.getSession(true); // Get the callback URL. String callbackUrl = String.format("http://%s:%d%s/oauth_callback", request.getServerName(), request.getServerPort(), request.getContextPath()); try { // Get the request token. RequestToken requestToken = twitter.getOAuthRequestToken(callbackUrl); // Store the request token for later use. session.setAttribute("requestToken", requestToken); // Save the Twitter client for later. session.setAttribute("twitterClient", twitter); // Redirect to Twitter authentication page. response.sendRedirect(requestToken.getAuthenticationURL()); } catch (TwitterException e) { logger.log(Level.SEVERE, "Could not login via Twitter: ", e); // Failure. Go back to home. Maybe add an error message. request.getRequestDispatcher("/index.jsp").forward(request, response); } }
From source file:com.mobilesolutionworks.android.twitter.TwitterPluginFragment.java
License:Apache License
@SuppressWarnings("unchecked") protected Task<RequestToken> doGetAuthenticationURL(final Twitter instance) { final Task<RequestToken>.TaskCompletionSource source = Task.create(); new AsyncTask<Task<RequestToken>.TaskCompletionSource, Void, Object>() { @Override//w ww .j a v a2s.c om protected Object doInBackground(Task<RequestToken>.TaskCompletionSource... params) { try { return instance.getOAuthRequestToken("oauth://works-twitter"); } catch (TwitterException e) { return e; } } @Override protected void onPostExecute(Object s) { super.onPostExecute(s); if (s instanceof RequestToken) { source.trySetResult((RequestToken) s); } else { source.trySetError((Exception) s); } } }.execute(source); return source.getTask(); }
From source file:com.mycompany.dovetune.SignIn.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//w w w . j a va2s . c om * * @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("Djgh7AmnNzxmv7fXzqlsdj8RB") .setOAuthConsumerSecret("Mnj40YllwKBBZvOkndnrmxl1TaNvcnS0x40PFhhDnMHysgzHum"); 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.mycompany.twitterdemo.SignIn.java
/** * Handles the HTTP <code>GET</code> method. * * @param request servlet request// w w w .j ava2s .c o m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Twitter twitter = new TwitterFactory().getInstance(); request.getSession().setAttribute("twitter", twitter); System.out.println("key:" + twitter.getConfiguration().getOAuthConsumerKey()); System.out.println("secret: " + twitter.getConfiguration().getOAuthConsumerSecret()); 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); } processRequest(request, response); }