List of usage examples for twitter4j.auth RequestToken getAuthenticationURL
public String getAuthenticationURL()
From source file:net.nitram509.controller.TwitterSignInHttpController.java
License:Open Source License
@POST @Produces({ TEXT_PLAIN })/*from w ww.j a v a 2 s . c om*/ public Response doTwitterSignIn(@Context HttpServletRequest request) throws TwitterException, IOException, URISyntaxException { RequestToken requestToken = retrieveTwitterRequestToken(request); new SessionVisitor(request.getSession(true)).saveRequestToken(requestToken.getToken()); String authenticationURL = requestToken.getAuthenticationURL(); return Response.seeOther(new URI(authenticationURL)).build(); }
From source file:net.wasdev.gameon.auth.twitter.TwitterAuth.java
License:Apache License
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ConfigurationBuilder c = new ConfigurationBuilder(); c.setOAuthConsumerKey(key).setOAuthConsumerSecret(secret); Twitter twitter = new TwitterFactory(c.build()).getInstance(); request.getSession().setAttribute("twitter", twitter); try {/*from w ww . j a va 2 s . c o m*/ // twitter will tell the users browser to go to this address once // they are done authing. StringBuffer callbackURL = request.getRequestURL(); int index = callbackURL.lastIndexOf("/"); callbackURL.replace(index, callbackURL.length(), "").append("/TwitterCallback"); // to initiate an auth request, twitter needs us to have a request // token. RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString()); // stash the request token in the session. request.getSession().setAttribute("requestToken", requestToken); // send the user to twitter to be authenticated. response.sendRedirect(requestToken.getAuthenticationURL()); } catch (TwitterException e) { throw new ServletException(e); } }
From source file:org.celstec.arlearn2.oauth.OauthTwitterWorker.java
License:Open Source License
public void redirectToTwitterForAuthentication(HttpServletRequest req, HttpServletResponse resp) throws IOException { Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(client_id, client_secret); RequestToken token = null; try {/*from w w w . j a v a 2 s.com*/ token = twitter.getOAuthRequestToken(redirect_uri); req.getSession().setAttribute("requestToken", token); String loginUrl = token.getAuthenticationURL() + "&force_login=true"; resp.sendRedirect(loginUrl); } catch (TwitterException e) { e.printStackTrace(); log.log(Level.SEVERE, e.getErrorMessage(), e); } }
From source file:org.eventjuggler.services.security.authc.social.twitter.TwitterAuthenticationMechanism.java
License:Open Source License
@Override protected Principal doAuthenticate(UserCredential credential, AuthenticationResult result) throws AuthenticationException { TwitterCredential oAuthCredential = (TwitterCredential) credential; HttpServletRequest request = oAuthCredential.getRequest(); HttpServletResponse response = oAuthCredential.getResponse(); HttpSession session = request.getSession(); Principal principal = null;/*from ww w .ja v a 2 s . c o m*/ provideSecrets(oAuthCredential); Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(clientID, clientSecret); //See if we are a callback RequestToken requestToken = (RequestToken) session.getAttribute(TWIT_REQUEST_TOKEN_SESSION_ATTRIBUTE); String verifier = request.getParameter("oauth_verifier"); if (requestToken != null && verifier != null) { try { AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier); session.setAttribute("accessToken", accessToken); session.removeAttribute("requestToken"); } catch (TwitterException e) { throw new AuthenticationException(e); } try { principal = new TwitterPrincipal(twitter.verifyCredentials()); checkUserInStore((TwitterPrincipal) principal); } catch (TwitterException e) { throw new AuthenticationException(e); } return principal; } try { requestToken = twitter.getOAuthRequestToken(returnURL); session.setAttribute(TWIT_REQUEST_TOKEN_SESSION_ATTRIBUTE, requestToken); response.sendRedirect(requestToken.getAuthenticationURL()); } catch (Exception e) { throw new AuthenticationException(e); } return principal; }
From source file:org.gameontext.auth.twitter.TwitterAuth.java
License:Apache License
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ConfigurationBuilder c = new ConfigurationBuilder(); c.setOAuthConsumerKey(key).setOAuthConsumerSecret(secret); Twitter twitter = new TwitterFactory(c.build()).getInstance(); request.getSession().setAttribute("twitter", twitter); try {//from w w w. j ava2 s . c o m // twitter will tell the users browser to go to this address once // they are done authing. String callbackURL = authURL + "/TwitterCallback"; // to initiate an auth request, twitter needs us to have a request // token. RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL); // stash the request token in the session. request.getSession().setAttribute("requestToken", requestToken); // send the user to twitter to be authenticated. response.sendRedirect(requestToken.getAuthenticationURL()); } catch (TwitterException e) { throw new ServletException(e); } }
From source file:org.gatein.security.oauth.twitter.TwitterProcessorImpl.java
License:Open Source License
@Override public InteractionState<TwitterAccessTokenContext> processOAuthInteraction(HttpServletRequest request, HttpServletResponse response) throws IOException, OAuthException { Twitter twitter = twitterFactory.getInstance(); HttpSession session = request.getSession(); //See if we are a callback RequestToken requestToken = (RequestToken) session .getAttribute(OAuthConstants.ATTRIBUTE_TWITTER_REQUEST_TOKEN); try {/*from www .java 2 s . c o m*/ if (requestToken == null) { requestToken = twitter.getOAuthRequestToken(redirectURL); // Save requestToken to session, but only temporarily until oauth workflow is finished session.setAttribute(OAuthConstants.ATTRIBUTE_TWITTER_REQUEST_TOKEN, requestToken); if (log.isTraceEnabled()) { log.trace("RequestToken obtained from twitter. Redirecting to Twitter for authorization"); } // Redirect to twitter to perform authentication response.sendRedirect(requestToken.getAuthenticationURL()); return new InteractionState<TwitterAccessTokenContext>(InteractionState.State.AUTH, null); } else { String verifier = request.getParameter(OAuthConstants.OAUTH_VERIFIER); // User denied scope if (request.getParameter(OAuthConstants.OAUTH_DENIED) != null) { throw new OAuthException(OAuthExceptionCode.USER_DENIED_SCOPE, "User denied scope on Twitter authorization page"); } // Obtain accessToken from twitter AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier); if (log.isTraceEnabled()) { log.trace("Twitter accessToken: " + accessToken); } // Remove requestToken from session. We don't need it anymore session.removeAttribute(OAuthConstants.ATTRIBUTE_TWITTER_REQUEST_TOKEN); TwitterAccessTokenContext accessTokenContext = new TwitterAccessTokenContext(accessToken.getToken(), accessToken.getTokenSecret()); return new InteractionState<TwitterAccessTokenContext>(InteractionState.State.FINISH, accessTokenContext); } } catch (TwitterException twitterException) { throw new OAuthException(OAuthExceptionCode.TWITTER_ERROR, twitterException); } }
From source file:org.jetbrains.webdemo.servlet.TwitterServlet.java
License:Apache License
private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException { try {/*from w ww . j a v a 2 s .c om*/ Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(apiKey, apiSecret); request.getSession().setAttribute("twitter", twitter); String callbackURL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/twitter/tweet"; request.getSession().setAttribute("tweetText", request.getParameter("tweet-text")); request.getSession().setAttribute("kotlin-level", request.getParameter("kotlin-level")); RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL); request.getSession().setAttribute("requestToken", requestToken); response.sendRedirect(requestToken.getAuthenticationURL()); } catch (Throwable e) { throw new ServletException(e); } }
From source file:org.jwebsocket.plugins.twitter.TwitterPlugIn.java
License:Apache License
private void requestAccessToken(WebSocketConnector aConnector, Token aToken) { TokenServer lServer = getServer();/*from w ww . java 2 s. c om*/ // instantiate response token Token lResponse = lServer.createResponse(aToken); String lMsg; String lCallbackURL = aToken.getString("callbackURL"); try { if (!mCheckAuth(lResponse)) { mLog.error(lResponse.getString("msg")); } else { // get a new Twitter object for the user TwitterFactory lTwitterFactory = new TwitterFactory(); Twitter lTwitter = lTwitterFactory.getInstance(); lTwitter.setOAuthConsumer(mSettings.getConsumerKey(), mSettings.getConsumerSecret()); // pass callback URL to Twitter API if given RequestToken lReqToken = (lCallbackURL != null ? lTwitter.getOAuthRequestToken(lCallbackURL) : lTwitter.getOAuthRequestToken()); String lAuthenticationURL = lReqToken.getAuthenticationURL(); String lAuthorizationURL = lReqToken.getAuthorizationURL(); lResponse.setString("authenticationURL", lAuthenticationURL); lResponse.setString("authorizationURL", lAuthorizationURL); lMsg = "authenticationURL: " + lAuthenticationURL + ", authorizationURL: " + lAuthorizationURL; lResponse.setString("msg", lMsg); if (mLog.isInfoEnabled()) { mLog.info(lMsg); } aConnector.setVar(OAUTH_REQUEST_TOKEN, lReqToken); aConnector.setVar(TWITTER_VAR, lTwitter); } } catch (Exception lEx) { lMsg = lEx.getClass().getSimpleName() + ": " + lEx.getMessage(); mLog.error(lMsg); lResponse.setInteger("code", -1); lResponse.setString("msg", lMsg); } // send response to requester lServer.sendToken(aConnector, lResponse); }
From source file:org.kawalpemilukada.login.login.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./* w ww . j a v a2 s . com*/ * * @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 { String form_action = request.getParameter("form_action"); if (form_action == null) { form_action = ""; } PrintWriter out = response.getWriter(); if (form_action.equalsIgnoreCase("loginfb")) { String tahun = request.getParameter("tahun"); if (tahun == null) { tahun = ""; } Facebook facebook = new FacebookFactory().getInstance(); request.getSession().setAttribute("facebook", facebook); request.getSession().setAttribute("tahun", tahun); StringBuffer callbackURL = request.getRequestURL(); int index = callbackURL.lastIndexOf("/"); callbackURL.replace(index, callbackURL.length(), "").append("/callbackfb"); response.sendRedirect(facebook.getOAuthAuthorizationURL(callbackURL.toString()) + "&display=popup"); } if (form_action.equalsIgnoreCase("logintwit")) { String tahun = request.getParameter("tahun"); if (tahun == null) { tahun = ""; } Twitter twitter = new TwitterFactory().getInstance(); request.getSession().setAttribute("twitter", twitter); request.getSession().setAttribute("tahun", tahun); String rurl = request.getParameter("rurl"); StringBuffer callbackURL = request.getRequestURL(); int index = callbackURL.lastIndexOf("/"); callbackURL.replace(index, callbackURL.length(), "").append("/callbackTwit?rurl=" + rurl); try { RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString()); request.getSession().setAttribute("requestToken", requestToken); response.sendRedirect(requestToken.getAuthenticationURL()); } catch (TwitterException e) { request.getSession().removeAttribute("twitter"); request.getSession().removeAttribute("tahun"); } } if (form_action.equalsIgnoreCase("loginmobiletwit")) { String t = "twit"; UserData user = CommonServices.getUser(request); LinkedHashMap record = new LinkedHashMap(); Gson gson = new Gson(); StringBuffer sb = new StringBuffer(); String line = null; BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) { sb.append(line); } JSONObject input = (JSONObject) JSONValue.parse(sb.toString()); if (user == null) { try { JSONObject twit = (JSONObject) input.get("user"); String id = CommonServices.getVal(twit.get("id")); id = id.replaceAll(t, ""); user = ofy().load().type(UserData.class).id(t + id).now(); if (user == null) { String accessToken = CommonServices.getVal(twit.get("token")); String accessTokenSecret = CommonServices.getVal(twit.get("secret")); String consumerKey = new CommonServices().getPropValues("kpu.properties", "consumerKey", request); String consumerSecret = new CommonServices().getPropValues("kpu.properties", "consumerSecret", request); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey) .setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(accessToken) .setOAuthAccessTokenSecret(accessTokenSecret); Twitter twitter = new TwitterFactory(cb.build()).getInstance(); User u = twitter.showUser(twitter.getId()); user = new UserData("twit" + CommonServices.getVal(twitter.getId())); user.imgurl = u.getBiggerProfileImageURLHttps().replace("http://", "https://"); user.nama = CommonServices.getVal(u.getName()); user.link = "https://twitter.com/" + CommonServices.getVal(twitter.getScreenName()); user.email = ""; user.uuid = CommonServices.getVal(twit.get("uuid")); user.type = t; ofy().save().entity(user).now(); Dashboard dashboard = CommonServices.getDashboard(CommonServices.setParentId("2015", "0")); Dashboard dashboard2014 = CommonServices .getDashboard(CommonServices.setParentId("2014", "0")); dashboard.users = CommonServices.getuserSize() + ""; ofy().save().entity(dashboard).now(); dashboard2014.users = dashboard.users + ""; ofy().save().entity(dashboard2014).now(); } else { user.lastlogin = CommonServices.JakartaTime(); user.uuid = CommonServices.getVal(twit.get("uuid")); if (user.link.equalsIgnoreCase("https://twitter.com/kawalpemilukada") || user.link.equalsIgnoreCase( "https://www.facebook.com/app_scoped_user_id/10153164750839760/")) { user.userlevel = 100000000; } ofy().save().entity(user).now(); } request.getSession().setAttribute("UserData", JSONValue.parse(gson.toJson(user))); MobileSession mobileSession = new MobileSession(user.uid + "#" + user.uuid); JSONObject device = (JSONObject) input.get("device"); mobileSession.platform = CommonServices.getVal(device.get("platform")); mobileSession.version = CommonServices.getVal(device.get("version")); mobileSession.cordova = CommonServices.getVal(device.get("cordova")); mobileSession.model = CommonServices.getVal(device.get("model")); mobileSession.manufacturer = CommonServices.getVal(device.get("manufacturer")); mobileSession.appversion = CommonServices.getVal(device.get("appversion")); ofy().save().entity(mobileSession).now(); record.put("sessionid", mobileSession.uuid); } catch (Exception e) { } record.put("sumber", "bukan dari session"); } else { MobileSession mobileSession = new MobileSession(user.uid + "#" + user.uuid); JSONObject device = (JSONObject) input.get("device"); mobileSession.platform = CommonServices.getVal(device.get("platform")); mobileSession.version = CommonServices.getVal(device.get("version")); mobileSession.cordova = CommonServices.getVal(device.get("cordova")); mobileSession.model = CommonServices.getVal(device.get("model")); mobileSession.manufacturer = CommonServices.getVal(device.get("manufacturer")); mobileSession.appversion = CommonServices.getVal(device.get("appversion")); ofy().save().entity(mobileSession).now(); record.put("sessionid", mobileSession.uuid); record.put("sumber", "dari session"); } record.put("user", JSONValue.parse(gson.toJson(user))); response.setContentType("text/html;charset=UTF-8"); out.print(JSONValue.toJSONString(record)); out.flush(); } if (form_action.equalsIgnoreCase("loginmobilefb")) { UserData user = CommonServices.getUser(request); LinkedHashMap record = new LinkedHashMap(); Gson gson = new Gson(); StringBuffer sb = new StringBuffer(); String line = null; BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) { sb.append(line); } JSONObject input = (JSONObject) JSONValue.parse(sb.toString()); if (user == null) { String t = "fb"; JSONObject fb = (JSONObject) input.get("user"); String id = CommonServices.getVal(fb.get("id")); id = id.replaceAll(t, ""); try { user = ofy().load().type(UserData.class).id(t + id).now(); if (user == null) { user = new UserData("fb" + CommonServices.getVal(fb.get("id"))); user.imgurl = "https://graph.facebook.com/" + fb.get("id") + "/picture"; user.nama = CommonServices.getVal(fb.get("name")); user.link = CommonServices.getVal(fb.get("link")); user.email = CommonServices.getVal(fb.get("email")); user.type = t; user.uuid = CommonServices.getVal(fb.get("uuid")); ofy().save().entity(user).now(); Dashboard dashboard = CommonServices.getDashboard(CommonServices.setParentId("2015", "0")); Dashboard dashboard2014 = CommonServices .getDashboard(CommonServices.setParentId("2014", "0")); dashboard.users = CommonServices.getuserSize() + ""; ofy().save().entity(dashboard).now(); dashboard2014.users = dashboard.users + ""; ofy().save().entity(dashboard2014).now(); } else { user.lastlogin = CommonServices.JakartaTime(); user.uuid = CommonServices.getVal(fb.get("uuid")); if (user.link.equalsIgnoreCase("https://twitter.com/kawalpemilukada") || user.link.equalsIgnoreCase( "https://www.facebook.com/app_scoped_user_id/10153164750839760/")) { user.userlevel = 100000000; } ofy().save().entity(user).now(); } request.getSession().setAttribute("UserData", JSONValue.parse(gson.toJson(user))); MobileSession mobileSession = new MobileSession(user.uid + "#" + user.uuid); JSONObject device = (JSONObject) input.get("device"); mobileSession.platform = CommonServices.getVal(device.get("platform")); mobileSession.version = CommonServices.getVal(device.get("version")); mobileSession.cordova = CommonServices.getVal(device.get("cordova")); mobileSession.model = CommonServices.getVal(device.get("model")); mobileSession.manufacturer = CommonServices.getVal(device.get("manufacturer")); mobileSession.appversion = CommonServices.getVal(device.get("appversion")); ofy().save().entity(mobileSession).now(); record.put("sessionid", mobileSession.uuid); } catch (Exception e) { } record.put("sumber", "bukan dari session"); } else { record.put("sumber", "dari session"); MobileSession mobileSession = new MobileSession(user.uid + "#" + user.uuid); JSONObject device = (JSONObject) input.get("device"); mobileSession.platform = CommonServices.getVal(device.get("platform")); mobileSession.version = CommonServices.getVal(device.get("version")); mobileSession.cordova = CommonServices.getVal(device.get("cordova")); mobileSession.model = CommonServices.getVal(device.get("model")); mobileSession.manufacturer = CommonServices.getVal(device.get("manufacturer")); mobileSession.appversion = CommonServices.getVal(device.get("appversion")); ofy().save().entity(mobileSession).now(); record.put("sessionid", mobileSession.uuid); } record.put("user", JSONValue.parse(gson.toJson(user))); response.setContentType("text/html;charset=UTF-8"); out.print(JSONValue.toJSONString(record)); out.flush(); } if (form_action.equalsIgnoreCase("cekauth")) { UserData user = CommonServices.getUser(request); LinkedHashMap record = new LinkedHashMap(); Gson gson = new Gson(); if (user == null) { record.put("status", "belum login"); } else { record.put("user", JSONValue.parse(gson.toJson(user))); if (user.terverifikasi.equalsIgnoreCase("Y")) { record.put("status", "terverifikasi"); } else { record.put("status", "Data Anda belum terverifikasi."); } } response.setContentType("text/html;charset=UTF-8"); out.print(JSONValue.toJSONString(record)); out.flush(); } if (form_action.equalsIgnoreCase("verifikasi")) { LinkedHashMap record = new LinkedHashMap(); StringBuffer sb = new StringBuffer(); String line = null; BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) { sb.append(line); } JSONArray input = (JSONArray) JSONValue.parse(sb.toString()); String nama = ""; String jenis_kelamin = ""; String no_tps = ""; try { String url = new CommonServices().getPropValues("kpu.properties", "verifikasiURL", request); String inputx = getWeb(url + input.get(0).toString()); JSONObject data = (JSONObject) JSONValue.parse(inputx); nama = data.get("nama").toString(); jenis_kelamin = data.get("jenis_kelamin").toString(); } catch (Exception e) { nama = ""; } if (nama == null || nama.equalsIgnoreCase("")) { try { String url = new CommonServices().getPropValues("kpu.properties", "verifikasiURL2015", request); String inputx = getWeb(url + input.get(0).toString()); LinkedHashMap data = getJsonNik(inputx); nama = data.get("nama").toString(); jenis_kelamin = data.get("jenis kelamin").toString(); no_tps = data.get("no_tps").toString(); } catch (Exception e) { nama = ""; } } JSONArray matchs = new JSONArray(); if (nama.equalsIgnoreCase(input.get(1).toString())) { UserData user = CommonServices.getUser(request); double match = 0; //if (data.get("nama").toString().equalsIgnoreCase(user.nama)) { //match = 100; //} else { String[] sosialnamaParts = user.nama.toString().split(" "); String[] namaParts = nama.toString().split(" "); int dibagi = sosialnamaParts.length; if (namaParts.length > dibagi) { dibagi = namaParts.length; } double increase = 100 / dibagi; String iiString = ""; for (int i = 0; i < namaParts.length; i++) { String namaPart = namaParts[i]; for (int ii = 0; ii < sosialnamaParts.length; ii++) { String fbnamaPart = sosialnamaParts[ii]; if (namaPart.equalsIgnoreCase(fbnamaPart) && (!iiString.contains(ii + ""))) { match = match + increase; matchs.add(namaPart + " sesuai dengan " + fbnamaPart); iiString = iiString + "[" + ii + "]"; break; } } } // } if (match > 100) { match = 100; } if (match >= 60) { user.terverifikasi = "Y"; user.notps = no_tps; ObjectifyService.ofy().save().entity(user).now(); Gson gson = new Gson(); record.put("user", JSONValue.parse(gson.toJson(user))); } record.put("status", match + ""); record.put("matchs", matchs); } else { record.put("status", "Data NIK: " + input.get(0).toString() + " dan Nama: " + input.get(1).toString() + " tidak ditemukan."); } response.setContentType("text/html;charset=UTF-8"); out.print(JSONValue.toJSONString(record)); out.flush(); } }
From source file:org.keycloak.social.twitter.TwitterIdentityProvider.java
License:Open Source License
@Override public Response performLogin(AuthenticationRequest request) { try {//from ww w . java 2 s. c om Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(getConfig().getClientId(), getConfig().getClientSecret()); URI uri = new URI(request.getRedirectUri() + "?state=" + request.getState()); RequestToken requestToken = twitter.getOAuthRequestToken(uri.toString()); ClientSessionModel clientSession = request.getClientSession(); clientSession.setNote("twitter_token", requestToken.getToken()); clientSession.setNote("twitter_tokenSecret", requestToken.getTokenSecret()); URI authenticationUrl = URI.create(requestToken.getAuthenticationURL()); return Response.temporaryRedirect(authenticationUrl).build(); } catch (Exception e) { throw new IdentityBrokerException("Could send authentication request to twitter.", e); } }