Skip to content

Instantly share code, notes, and snippets.

@bh5k
Created August 10, 2015 03:54
Show Gist options
  • Save bh5k/73a82d64e35e780150d1 to your computer and use it in GitHub Desktop.
Save bh5k/73a82d64e35e780150d1 to your computer and use it in GitHub Desktop.
package com.pluralsight.proxy;
import java.util.List;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
public class TwitterServiceImpl implements TwitterService {
private static final String TWITTER_CONSUMER_KEY = "PUT_YOUR_VALUES_HERE";
private static final String TWITTER_SECRET_KEY = "PUT_YOUR_VALUES_HERE";
private static final String TWITTER_ACCESS_TOKEN = "PUT_YOUR_VALUES_HERE";
private static final String TWITTER_ACCESS_TOKEN_SECRET = "PUT_YOUR_VALUES_HERE";
@Override
public String getTimeline(String screenName) {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
.setOAuthConsumerSecret(TWITTER_SECRET_KEY)
.setOAuthAccessToken(TWITTER_ACCESS_TOKEN)
.setOAuthAccessTokenSecret(TWITTER_ACCESS_TOKEN_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
StringBuilder builder = new StringBuilder();
try {
Query query = new Query(screenName);
QueryResult result;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
builder.append("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
builder.append("\n");
}
} while ((query = result.nextQuery()) != null);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
}
return builder.toString();
}
@Override
public void postToTimeline(String screenName, String message) {
//we aren't going to allow this
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment