List of usage examples for twitter4j.auth RequestToken getAuthenticationURL
public String getAuthenticationURL()
From source file:org.keycloak.social.twitter.TwitterProvider.java
License:Open Source License
@Override public AuthRequest getAuthUrl(SocialProviderConfig config, String state) throws SocialProviderException { try {/*from ww w . j a v a2 s. com*/ Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(config.getKey(), config.getSecret()); URI uri = new URI(config.getCallbackUrl() + "?state=" + state); RequestToken requestToken = twitter.getOAuthRequestToken(uri.toString()); return AuthRequest.create(requestToken.getAuthenticationURL()) .setAttribute("token", requestToken.getToken()) .setAttribute("tokenSecret", requestToken.getTokenSecret()).build(); } catch (Exception e) { throw new SocialProviderException(e); } }
From source file:org.picketlink.extensions.core.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 w w w .j a va 2 s . co m 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); if (requestToken != null) { String verifier = request.getParameter("oauth_verifier"); 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.picketlink.social.auth.TwitterAuthenticator.java
License:Apache License
@Override public void authenticate() { if (httpServletRequest == null) { throw new IllegalStateException("http request not available"); }//from ww w. j ava 2 s . co m if (httpServletResponse == null) { throw new IllegalStateException("http response not available"); } if (configuration == null) { throw new IllegalStateException("configuration not available"); } HttpSession session = httpServletRequest.getSession(); ServletContext servletContext = httpServletRequest.getServletContext(); String clientID = configuration.getClientID(); String clientSecret = configuration.getClientSecret(); String returnURL = configuration.getReturnURL(); Principal principal = null; Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(clientID, clientSecret); //See if we are a callback String verifier = httpServletRequest.getParameter("oauth_verifier"); RequestToken requestToken = (RequestToken) session.getAttribute(TWIT_REQUEST_TOKEN_SESSION_ATTRIBUTE); if (verifier != null && requestToken == null) { //Let us fall back String twitterSentRequestToken = httpServletRequest.getParameter("oauth_token"); if (twitterSentRequestToken != null) { requestToken = (RequestToken) servletContext.getAttribute(twitterSentRequestToken); } if (requestToken == null) { throw new IllegalStateException("Verifier present but request token null"); } //Discard the stored request tokens servletContext.removeAttribute(twitterSentRequestToken); session.removeAttribute(TWIT_REQUEST_TOKEN_SESSION_ATTRIBUTE); } 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("Twitter Login:", e); } try { principal = new TwitterPrincipal(twitter.verifyCredentials()); setStatus(AuthenticationStatus.SUCCESS); setAccount(new User(principal.getName())); return; } catch (TwitterException e) { throw new AuthenticationException("Twitter Login:", e); } } try { requestToken = twitter.getOAuthRequestToken(returnURL); session.setAttribute(TWIT_REQUEST_TOKEN_SESSION_ATTRIBUTE, requestToken); //back up in the case the browser provides a new session to the user on twitter callback servletContext.setAttribute(requestToken.getToken(), requestToken); httpServletResponse.sendRedirect(requestToken.getAuthenticationURL()); } catch (Exception e) { throw new AuthenticationException("Twitter Login:", e); } if (principal != null) { setStatus(AuthenticationStatus.SUCCESS); setAccount(new User(principal.getName())); } }
From source file:org.smarttechie.servlet.TwitterLoginServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("TwitterLoginServlet:doGet"); ConfigurationBuilder cb = new ConfigurationBuilder(); Properties props = new TwitterProperties().getProperties(); cb.setDebugEnabled(true).setOAuthConsumerKey((String) props.get("twitterConsumerKey")) .setOAuthConsumerSecret((String) props.get("twitterConsumerSecret")) .setOAuthRequestTokenURL((String) props.get("twitterRequestTokenURL")) .setOAuthAuthorizationURL((String) props.get("twitterAuthorizeURL")) .setOAuthAccessTokenURL((String) props.get("twitterAccessTokenURL")); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance();/*from w w w. jav a 2s . c o m*/ 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("/TwitterCallback"); 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:org.wso2.carbon.identity.authenticator.twitter.TwitterAuthenticator.java
License:Open Source License
/** * Initiate the authentication request//from w w w .j ava2 s . c om */ @Override protected void initiateAuthenticationRequest(HttpServletRequest request, HttpServletResponse response, AuthenticationContext context) throws AuthenticationFailedException { ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); Map<String, String> authenticatorProperties = context.getAuthenticatorProperties(); String apiKey = authenticatorProperties.get(TwitterAuthenticatorConstants.TWITTER_API_KEY); String apiSecret = authenticatorProperties.get(TwitterAuthenticatorConstants.TWITTER_API_SECRET); configurationBuilder.setIncludeEmailEnabled(true); Twitter twitter = new TwitterFactory(configurationBuilder.build()).getInstance(); twitter.setOAuthConsumer(apiKey, apiSecret); try { String queryParams = FrameworkUtils.getQueryStringWithFrameworkContextId(context.getQueryParams(), context.getCallerSessionKey(), context.getContextIdentifier()); String callbackURL = getCallbackUrl(authenticatorProperties); RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString()); String subStr = queryParams .substring(queryParams.indexOf(TwitterAuthenticatorConstants.TWITTER_SESSION_DATA_KEY + "=")); String sessionDK = subStr .substring(subStr.indexOf(TwitterAuthenticatorConstants.TWITTER_SESSION_DATA_KEY + "="), subStr.indexOf("&")) .replace((TwitterAuthenticatorConstants.TWITTER_SESSION_DATA_KEY + "="), ""); request.getSession().setAttribute(TwitterAuthenticatorConstants.TWITTER_SESSION_DATA_KEY, sessionDK); request.getSession().setAttribute(TwitterAuthenticatorConstants.TWITTER_REQUEST_TOKEN, requestToken); request.getSession().setAttribute(TwitterAuthenticatorConstants.AUTHENTICATOR_NAME.toLowerCase(), twitter); response.sendRedirect(requestToken.getAuthenticationURL()); } catch (TwitterException e) { log.error("Exception while sending to the Twitter login page.", e); throw new AuthenticationFailedException(e.getMessage(), e); } catch (IOException e) { log.error("Exception while sending to the Twitter login page.", e); throw new AuthenticationFailedException(e.getMessage(), e); } }
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 ww.j a v a 2s . co 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; try {/* w ww . j a va2 s .c o m*/ 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(); } }
From source file:twitter.signin.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*w w w. ja v a 2 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 { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { initConfig(); request.getSession().setAttribute("twitter", twitter); 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()); request.getSession().setAttribute("requestToken", requestToken); response.sendRedirect(requestToken.getAuthenticationURL()); } catch (Exception ex) { ex.printStackTrace(); } finally { out.close(); } }
From source file:ua.group06.logic.TwitterAuthentication.java
public String getauthenticationURL(/*String requestURL*/) throws TwitterException { ConfigurationBuilder cb = new ConfigurationBuilder(); // cb.setApplicationOnlyAuthEnabled(true); //cb.setOAuth2TokenType(token.getTokenType()); //cb.setOAuth2AccessToken(token.getAccessToken()); cb.setDebugEnabled(true).setOAuthConsumerKey(CONSUMER_KEY).setOAuthConsumerSecret(CONSUMER_SECRET); Twitter twitter = new TwitterFactory(cb.build()).getInstance(); this.twitter = twitter; //request.setAttribute("twitter", twitter); RequestToken requestToken = twitter.getOAuthRequestToken(/*requestURL*/); this.requestToken = requestToken; //request.getSession().setAttribute("requestToken", requestToken); return requestToken.getAuthenticationURL(); }