List of usage examples for twitter4j TwitterException getStatusCode
public int getStatusCode()
From source file:spammerwadgets.AutoOAuthAccessFromFile.java
License:Apache License
AccessToken getAccessToken(String username, String password) throws TwitterException, IOException { AccessToken accessToken = null;// w w w . ja v a 2s . c o m //ConfigurationBuilder cb = new ConfigurationBuilder(); //cb.setUseSSL(true); Twitter twitter = new TwitterFactory().getInstance(); RequestToken requestToken = twitter.getOAuthRequestToken(); System.out.println("Got request token."); System.out.println("Request token: " + requestToken.getToken()); System.out.println("Request token secret: " + requestToken.getTokenSecret()); while (null == accessToken) { System.out.println("Open the following URL and grant access to your account:"); String url = requestToken.getAuthorizationURL(); System.out.println(url); // Alternatively, we will generate the POST request automatically // Firstly, we parse the webpage of the authotizationURL Document authDoc = Jsoup.connect(url).get(); Element form = authDoc.getElementById("oauth_form"); System.out.println(form.toString()); String action = form.attr("action"); String authenticity_token = form.select("input[name=authenticity_token]").attr("value"); String oauth_token = form.select("input[name=oauth_token]").attr("value"); // Then submit the authentication webpage Document tokenDoc = Jsoup.connect(action).data("authenticity_token", authenticity_token) .data("oauth_token", oauth_token).data("session[username_or_email]", username) .data("session[password]", password).post(); // System.out.println(doc2.toString()); Element oauth_pin = tokenDoc.getElementsByTag("code").first(); if (oauth_pin == null) { System.out.println("Something wrong!!!!!!!"); return null; } String pin = oauth_pin.text(); try { if (pin.length() > 0) { accessToken = twitter.getOAuthAccessToken(requestToken, pin); } else { accessToken = twitter.getOAuthAccessToken(requestToken); } } catch (TwitterException te) { if (401 == te.getStatusCode()) { System.out.println("Unable to get the access token."); } else { te.printStackTrace(); } } } System.out.println("Got access token."); System.out.println("Username: " + username); System.out.println("Password: " + password); System.out.println("Access token: " + accessToken.getToken()); System.out.println("Access token secret: " + accessToken.getTokenSecret()); return accessToken; }
From source file:tkwatch.GetAccessTokens.java
License:Open Source License
public static void main(String args[]) throws Exception { // The factory instance is re-usable and thread safe. Twitter twitter = new TwitterFactory().getInstance(); // Assumes consumer key and consumer secret are already stored in // properties file. // twitter.setOAuthConsumer(Constants.CONSUMER_KEY, // Constants.CONSUMER_SECRET); RequestToken requestToken = twitter.getOAuthRequestToken(); AccessToken accessToken = null;/*w w w .j av a2 s .c o m*/ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (null == accessToken) { System.out.println("Open the following URL and grant access to your account:"); System.out.println(requestToken.getAuthorizationURL()); System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:"); String pin = br.readLine(); try { if (pin.length() > 0) { accessToken = twitter.getOAuthAccessToken(requestToken, pin); } else { accessToken = twitter.getOAuthAccessToken(); } } catch (TwitterException te) { if (401 == te.getStatusCode()) { System.out.println("Unable to get the access token."); } else { te.printStackTrace(); } } } // Print out the access tokens for future reference. System.out.println("Access tokens for " + twitter.verifyCredentials().getId()); System.out.println("oauth.accessToken=" + accessToken.getToken()); System.out.println("oauth.accessTokenSecret=" + accessToken.getTokenSecret()); }
From source file:tkwatch.Utilities.java
License:Open Source License
/** * The Twitter OAuth authorization process requires access tokens. Twitter4j * stores them in <code>twitter4j.properties</code>. Invoke this routine to * obtain them; then add the appropriate lines in the properties file. (You * will need to write your own <code>main()</code> to do this. This routine * isn't actually called from the <strong>TKWatch+</strong> routine. * Documentation for <code>twitter4j.properties</code> is available <a * href="http://twitter4j.org/en/configuration.html">here</a>. * <p>/* w w w.j a va2 s.co m*/ * Adapted from Yusuke Yamamoto, <a * href="http://twitter4j.org/en/code-examples.html">OAuth support</a>, * April 18, 2011. */ @SuppressWarnings("null") public static final void getAccessTokens() { // The factory instance is re-usable and thread safe. Twitter twitter = new TwitterFactory().getInstance(); // Assumes consumer key and consumer secret are already stored in // properties file. // twitter.setOAuthConsumer(Constants.CONSUMER_KEY, // Constants.CONSUMER_SECRET); RequestToken requestToken = null; try { requestToken = twitter.getOAuthRequestToken(); } catch (TwitterException e1) { e1.printStackTrace(); } AccessToken accessToken = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (null == accessToken) { System.out.println("Open the following URL and grant access to your account:"); System.out.println(requestToken.getAuthorizationURL()); System.out.print("Cut and paste the pin:"); String pin = ""; try { pin = br.readLine(); } catch (IOException e) { System.out.println(e.getMessage()); e.printStackTrace(); } try { if (pin.length() > 0) { accessToken = twitter.getOAuthAccessToken(requestToken, pin); } else { accessToken = twitter.getOAuthAccessToken(); } } catch (TwitterException te) { if (401 == te.getStatusCode()) { System.out.println("Unable to get the access token."); } else { te.printStackTrace(); } } } // Print out the access tokens for future reference. The ones we got are // in the // <code>twitter4j.properties</code> file. try { System.out.println("Access tokens for " + twitter.verifyCredentials().getId()); } catch (TwitterException e) { System.out.println(e.getMessage()); e.printStackTrace(); } System.out.println("Store in twitter4j.properties as oauth.accessToken=" + accessToken.getToken()); System.out.println( "Store in twitter4j.properties as oauth.accessTokenSecret=" + accessToken.getTokenSecret()); }
From source file:tweete.Tweete.java
License:Open Source License
public void updateTweete(String sta) { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("######################") .setOAuthConsumerSecret("######################") .setOAuthAccessToken("############################################") .setOAuthAccessTokenSecret("############################################"); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance();/*from www. j a v a2s. c o m*/ try { twitter.updateStatus(sta); System.out.println("Successfully updated the status in Twitter."); } catch (TwitterException te) { if (401 == te.getStatusCode()) { System.out.println("Unable to get the access token."); } else if (92 == te.getStatusCode()) { System.out.println("SSL is required"); } else { System.out.println("Failed to get timeline: " + te.getMessage()); System.exit(-1); } } catch (Exception e) { System.out.println("Something went wrong"); } }
From source file:tweete.Tweete.java
License:Open Source License
public void showTimeline() { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("######################") .setOAuthConsumerSecret("######################") .setOAuthAccessToken("############################################") .setOAuthAccessTokenSecret("############################################"); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance();/* w w w . jav a 2 s . c o m*/ try { ResponseList<Status> a = twitter.getUserTimeline(new Paging(1, 10)); String statuses = ""; for (Status b : a) { statuses = statuses + b.getText() + "\n\n---------------------------------------\n\n"; } new TweeteTimeline().Timeline(statuses); } catch (TwitterException te) { //te.printStackTrace(); if (401 == te.getStatusCode()) { System.out.println("Unable to get the access token."); } else if (92 == te.getStatusCode()) { System.out.println("SSL is required"); } else { System.out.println("Failed to get timeline: " + te.getMessage()); System.exit(-1); } } catch (Exception e) { System.out.println("Something went wrong"); } }
From source file:tweete.Tweete.java
License:Open Source License
public void sendMessage(String id, String msg) { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("######################") .setOAuthConsumerSecret("######################") .setOAuthAccessToken("############################################") .setOAuthAccessTokenSecret("############################################"); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance();//w w w. j a v a 2 s .c om try { DirectMessage message = null; message = twitter.sendDirectMessage(id, msg); System.out.println("Sent: " + message.getText() + " to @" + message.getRecipientScreenName()); } catch (TwitterException te) { //te.printStackTrace(); if (401 == te.getStatusCode()) { System.out.println("Unable to get the access token."); } else if (92 == te.getStatusCode()) { System.out.println("SSL is required"); } else { System.out.println("Failed to get timeline: " + te.getMessage()); System.exit(-1); } } catch (Exception e) { System.out.println("Something went wrong"); } }
From source file:twitter.sample.GetAccessToken.java
License:Apache License
/** * Usage: java twitter4j.examples.oauth.GetAccessToken [consumer key] [consumer secret] * * @param args message/*from ww w . j a v a2s . com*/ */ public static void main(String[] args) { File file = new File("twitter4j.properties"); Properties prop = new Properties(); InputStream is = null; OutputStream os = null; try { if (file.exists()) { is = new FileInputStream(file); prop.load(is); } if (args.length < 2) { if (null == prop.getProperty("oauth.consumerKey") && null == prop.getProperty("oauth.consumerSecret")) { // consumer key/secret are not set in twitter4j-bak.properties System.out.println( "Usage: java twitter4j.examples.oauth.GetAccessToken [consumer key] [consumer secret]"); System.exit(-1); } } else { prop.setProperty("oauth.consumerKey", args[0]); prop.setProperty("oauth.consumerSecret", args[1]); os = new FileOutputStream("twitter4j.properties"); prop.store(os, "twitter4j.properties"); } } catch (IOException ioe) { ioe.printStackTrace(); System.exit(-1); } finally { if (is != null) { try { is.close(); } catch (IOException ignore) { } } if (os != null) { try { os.close(); } catch (IOException ignore) { } } } try { Twitter twitter = new TwitterFactory().getInstance(); RequestToken requestToken = twitter.getOAuthRequestToken(); System.out.println("Got request token."); System.out.println("Request token: " + requestToken.getToken()); System.out.println("Request token secret: " + requestToken.getTokenSecret()); AccessToken accessToken = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (null == accessToken) { System.out.println("Open the following URL and grant access to your account:"); System.out.println(requestToken.getAuthorizationURL()); try { Desktop.getDesktop().browse(new URI(requestToken.getAuthorizationURL())); } catch (UnsupportedOperationException ignore) { } catch (IOException ignore) { } catch (URISyntaxException e) { throw new AssertionError(e); } System.out.print("Enter the PIN(if available) and hit enter after you granted access.[PIN]:"); String pin = br.readLine(); try { if (pin.length() > 0) { accessToken = twitter.getOAuthAccessToken(requestToken, pin); } else { accessToken = twitter.getOAuthAccessToken(requestToken); } } catch (TwitterException te) { if (401 == te.getStatusCode()) { System.out.println("Unable to get the access token."); } else { te.printStackTrace(); } } } System.out.println("Got access token."); System.out.println("Access token: " + accessToken.getToken()); System.out.println("Access token secret: " + accessToken.getTokenSecret()); try { prop.setProperty("oauth.accessToken", accessToken.getToken()); prop.setProperty("oauth.accessTokenSecret", accessToken.getTokenSecret()); os = new FileOutputStream(file); prop.store(os, "twitter4j.properties"); os.close(); } catch (IOException ioe) { ioe.printStackTrace(); System.exit(-1); } finally { if (os != null) { try { os.close(); } catch (IOException ignore) { } } } System.out.println("Successfully stored access token to " + file.getAbsolutePath() + "."); System.exit(0); } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to get accessToken: " + te.getMessage()); System.exit(-1); } catch (IOException ioe) { ioe.printStackTrace(); System.out.println("Failed to read the system input."); System.exit(-1); } }
From source file:twitter.UpdateStatus.java
License:Apache License
public boolean execute(String statusMessage) throws IOException { try {/*w w w . j a v a 2s . c o m*/ Twitter twitter = new TwitterFactory().getInstance(); try { // get request token. // this will throw IllegalStateException if access token is already available RequestToken requestToken = twitter.getOAuthRequestToken(); LOG.info("Got request token."); LOG.info("Request token: " + requestToken.getToken()); LOG.info("Request token secret: " + requestToken.getTokenSecret()); AccessToken accessToken = null; while (null == accessToken) { try { URI uri = new URI(requestToken.getAuthorizationURL()); Desktop.getDesktop().browse(uri); } catch (IOException e) { LOG.error(e); } catch (URISyntaxException e) { LOG.error(e); } JFrame frame = new JFrame("Twitter PIN verification"); // prompt the user to enter their name String pin = JOptionPane.showInputDialog(frame, "Enter the PIN"); if (pin == null) return false; try { if (pin.length() > 0) { accessToken = twitter.getOAuthAccessToken(requestToken, pin); } else { accessToken = twitter.getOAuthAccessToken(requestToken); } } catch (TwitterException te) { if (401 == te.getStatusCode()) { LOG.error("Unable to get the access token."); } else { LOG.error(te); } } } LOG.info("Got access token."); LOG.info("Access token: " + accessToken.getToken()); LOG.info("Access token secret: " + accessToken.getTokenSecret()); } catch (IllegalStateException ie) { // access token is already available, or consumer key/secret is not set. if (!twitter.getAuthorization().isEnabled()) { return false; } } twitter.updateStatus(statusMessage); } catch (TwitterException te) { LOG.error(te); return false; } return true; }
From source file:twitter4j.examples.list.ShowUserListMembership.java
License:Apache License
/** * Usage: java twitter4j.examples.list.ShowUserListMembership [list id] [user id] * * @param args message//from w ww. j av a 2 s .com */ public static void main(String[] args) { if (args.length < 2) { System.out.println("Usage: java twitter4j.examples.list.ShowUserListMembership [list id] [user id]"); System.exit(-1); } try { Twitter twitter = new TwitterFactory().getInstance(); long listId = Long.parseLong(args[0]); UserList list = twitter.showUserList(listId); long userId = Integer.parseInt(args[1]); User user = twitter.showUser(userId); try { twitter.showUserListMembership(listId, userId); System.out.println("@" + user.getScreenName() + " is in the list:" + list.getName()); } catch (TwitterException te) { if (te.getStatusCode() == 404) { System.out.println("@" + user.getScreenName() + " is not in the list:" + list.getName()); } } System.exit(0); } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to check user membership: " + te.getMessage()); System.exit(-1); } }
From source file:twitter4j.examples.list.ShowUserListSubscription.java
License:Apache License
/** * Usage: java twitter4j.examples.list.ShowUserListSubscription [list id] [user id] * * @param args message//from www .j av a 2s. c o m */ public static void main(String[] args) { if (args.length < 2) { System.out.println("Usage: java twitter4j.examples.list.ShowUserListSubscription [list id] [user id]"); System.exit(-1); } try { Twitter twitter = new TwitterFactory().getInstance(); long listId = Long.parseLong(args[0]); UserList list = twitter.showUserList(listId); long userId = Integer.parseInt(args[1]); User user = twitter.showUser(userId); try { twitter.showUserListSubscription(listId, userId); System.out.println("@" + user.getScreenName() + " is subscribing the list:" + list.getName()); } catch (TwitterException te) { if (te.getStatusCode() == 404) { System.out.println( "@" + user.getScreenName() + " is not subscribing the list:" + list.getName()); } } System.exit(0); } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to check user subscription: " + te.getMessage()); System.exit(-1); } }