/* Twitter Authorization Example: Hard Coded Credentials This program makes a trivial call to the Twitter API to demonstrate how you can hard code credentials into an application. In general, you would consider hard coding credentials for server applications. */ import twitter4j.*; import twitter4j.auth.AccessToken; public class Main { // Change the "..." in the following to the values for your app and user private static final String CONSUMER_KEY = "..."; private static final String CONSUMER_SECRET = "..."; private static final String ACCESS_TOKEN = "..."; private static final String ACCESS_TOKEN_SECRET = "..."; public static void main(String[] args) { // Get the root twitter object Twitter twitter = TwitterFactory.getSingleton(); // Set up the access tokens and keys to get permission to access twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); AccessToken at = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET); twitter.setOAuthAccessToken(at); // Now do a simple search to show that the tokens work try { Query q = new Query("java programming"); // Search for tweets that contain these two words q.setCount(100); // Let's get all the tweets we can in one call q.resultType("recent"); // Get all tweets q.setLang("en"); // English language tweets, please QueryResult r = twitter.search(q); // Make the call for (Status s: r.getTweets()) // Loop through all the tweets... { System.out.printf("At %s, @%-20s said: %s\n", s.getCreatedAt().toString(), s.getUser().getScreenName(), s.getText()); } } catch (TwitterException e) { System.out.println("That didn't work well...wonder why?"); e.printStackTrace(); } // That's all, folks! } }