List of usage examples for twitter4j Twitter getOAuthAccessToken
AccessToken getOAuthAccessToken(String oauthVerifier) throws TwitterException;
From source file:com.allenzheng.twittersyn.controller.AccountServlet.java
License:Apache License
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ String action = request.getParameter(PARAM_ACTION); /* w ww. j a v a2 s .c o m*/ HttpSession session = request.getSession(); if(ACTION_TWITTER_SIGN_IN.equals(action)){ logger.debug("Signing in with Twitter..."); TwitterAPIImpl twitterapi = new TwitterAPIImpl(); String id = null; Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) { Cookie cookie; cookie = cookies[i]; if (COOKIE_TWITTER_ID.equals(cookie.getName())) { id = cookie.getValue(); } } AccessToken accessToken = null; if (id != null){ } } String oauthToken = request.getParameter(PARAM_OAUTH_TOKEN); if(oauthToken != null){ logger.debug(PARAM_OAUTH_TOKEN + " received from Twitter"); try { Twitter twitter = (Twitter) session.getAttribute(ATTR_TWITTER); RequestToken requestToken = (RequestToken) session .getAttribute(ATTR_REQUEST_TOKEN); AccessToken accessToken; if (callbackUrl == null) { accessToken = twitter.getOAuthAccessToken(requestToken); } else { String oauthVerifier = request .getParameter(PARAM_OAUTH_VERIFIER); logger.debug(PARAM_OAUTH_VERIFIER + " received from Twitter"); accessToken = twitter.getOAuthAccessToken(requestToken.getToken(), requestToken.getTokenSecret(), oauthVerifier); } twitter.setOAuthAccessToken(accessToken); session.removeAttribute(ATTR_REQUEST_TOKEN); session.setAttribute(ATTR_TWITTER, twitter); int id = twitter.verifyCredentials().getId(); logger.debug("Access token retrieved for user " + id + " from Twitter"); // storeAccessToken(id, accessToken); Cookie cookie = new Cookie(COOKIE_TWITTER_ID, "" + id); cookie.setMaxAge(63072000); // Valid for 2 years response.addCookie(cookie); logger.debug("Cookie set for user " + id); // Get last status and friends' timelines // getMyLastStatusAndStoreInSession(session); // getFriendsTimelinesAndStoreInSession(session); // Go to the update status page // request.getRequestDispatcher(PAGE_UPDATE_STATUS).forward( // request, response); } catch (TwitterException e) { logger.error("Failed to retrieve access token - " + e.getMessage()); throw new ServletException(e); } } }
From source file:com.amandine.twitterpostforcoucou.Tweet.java
private void publishStatusUpdateWithMedia(String message) throws MalformedURLException, IOException { Status status = null;// ww w.j a v a 2 s . co m try { Twitter twitter = new TwitterFactory().getInstance(); try { RequestToken requestToken = twitter.getOAuthRequestToken(); AccessToken accessToken = null; while (null == accessToken) { logger.fine("Open the following URL and grant access to your account:"); logger.fine(requestToken.getAuthorizationURL()); try { accessToken = twitter.getOAuthAccessToken(requestToken); } catch (TwitterException te) { if (401 == te.getStatusCode()) { logger.severe("Unable to get the access token."); } else { te.printStackTrace(); } } } logger.log(Level.INFO, "Got access token."); logger.log(Level.INFO, "Access token: {0}", accessToken.getToken()); logger.log(Level.INFO, "Access token secret: {0}", accessToken.getTokenSecret()); } catch (IllegalStateException ie) { // access token is already available, or consumer key/secret is not set. if (!twitter.getAuthorization().isEnabled()) { logger.severe("OAuth consumer key/secret is not set."); return; } } //Instantiate and initialize a new twitter status update StatusUpdate statusUpdate = new StatusUpdate(message); //attach any media, if you want to statusUpdate.setMedia( //title of media "Amandine Leforestier Spring Summer 2015 white", new URL("https://issuu.com/kadiemurphy/docs/volume_xxi_issuu/52?e=0").openStream()); //tweet or update status status = twitter.updateStatus(statusUpdate); //Status status = twitter.updateStatus(message); logger.log(Level.INFO, "Successfully updated the status to [{0}].", status.getText()); } catch (TwitterException te) { te.printStackTrace(); logger.log(Level.SEVERE, "Failed to get timeline: {0}", te.getMessage()); } }
From source file:com.amandine.twitterpostforcoucou.Tweet.java
public Twitter getTwitter() { Twitter twitter = new TwitterFactory().getInstance(); try {/*w w w . j a v a2s.c o m*/ RequestToken requestToken = twitter.getOAuthRequestToken(); AccessToken accessToken = null; while (null == accessToken) { logger.fine("Open the following URL and grant access to your account:"); logger.fine(requestToken.getAuthorizationURL()); try { accessToken = twitter.getOAuthAccessToken(requestToken); } catch (TwitterException te) { if (401 == te.getStatusCode()) { logger.severe("Unable to get the access token."); } else { logger.log(Level.SEVERE, "Failed to get timeline: {0}", te.getMessage()); } } } logger.log(Level.INFO, "Got access token."); logger.log(Level.INFO, "Access token: {0}", accessToken.getToken()); logger.log(Level.INFO, "Access token secret: {0}", accessToken.getTokenSecret()); } catch (IllegalStateException ie) { // access token is already available, or consumer key/secret is not set. if (!twitter.getAuthorization().isEnabled()) { logger.log(Level.SEVERE, "OAuth consumer key/secret is not set: {0}", ie.getMessage()); return null; } } catch (TwitterException te) { logger.log(Level.SEVERE, "OAuth request token: {0}", te.getMessage()); } return twitter; }
From source file:com.anshul.HomeServlet.java
License:Open Source License
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { Twitter twitter = (Twitter) req.getSession().getAttribute("twitter"); if (twitter != null) { String oauth_verifier = req.getParameter("oauth_verifier"); if (oauth_verifier != null) { try { AccessToken accessToken; accessToken = twitter.getOAuthAccessToken(oauth_verifier); twitter.setOAuthAccessToken(accessToken); statusCheck(twitter, req, resp); } catch (TwitterException e) { // TODO Auto-generated catch block e.printStackTrace();//from ww w . j av a 2 s . com } } else { try { statusCheck(twitter, req, resp); } catch (TwitterException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
From source file:com.gmail.altakey.joanne.service.TwitterAuthService.java
License:Apache License
private void authenticateDone(final Twitter tw, final String verifier) { try {//w ww.ja v a 2 s .c o m final AccessToken accessToken = tw.getOAuthAccessToken(verifier); Log.d(TAG, String.format("got access token: %s", accessToken.toString())); setAccessToken(accessToken); updateRelations(this, accessToken); final Intent intent = new Intent(ACTION_AUTH_SUCCESS); intent.putExtra(EXTRA_TOKEN, accessToken); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } catch (TwitterException e) { Log.e(TAG, "authentication failure", e); final Intent intent = new Intent(ACTION_AUTH_FAIL); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } }
From source file:org.sush.twitterstream.MyTwitterStream.java
License:Apache License
private AccessToken getAccessToken() throws TwitterException, URISyntaxException, IOException { Twitter twitter = new TwitterFactory(configurationBuilder.build()).getInstance(); RequestToken requestToken = twitter.getOAuthRequestToken(); Desktop.getDesktop().browse(new URI(requestToken.getAuthorizationURL())); AccessToken accessToken = twitter.getOAuthAccessToken(requestToken); return accessToken; }