指定ユーザのタイムラインのツイートを取得
今回は指定ユーザのタイムラインのツイートを取得するソースコードを書きます.
import twitter4j.Paging; import twitter4j.ResponseList; import twitter4j.Status; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; import twitter4j.api.TimelinesResources; import twitter4j.auth.AccessToken; public class Print { public static void main(String[] args) { // TODO Auto-generated method stub String consumerKey = "自分のconsumerKey"; String consumerSecret = "自分のconsumerSecret"; String accessToken = "自分のaccessToken"; String accessTokenSecret = "自分のaccessTokenSecret"; Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(consumerKey, consumerSecret); twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret)); try { TimelinesResources timeline = twitter.timelines(); Paging paging = new Paging();// Pagingオブジェクトを作成 paging.setPage(1);// ページ番号を指定 paging.count(200);// 1ページから取得するツイート数を指定 // paging.setMaxId(ツイートのID);// MaxIdよりも後のツイートを取得するよう指定 // paging.setSinceId(sinceId);// SinceIdよりも前のツイートを取得するよう指定 ResponseList tweets = timeline.getUserTimeline("指定ユーザ名", paging); for(Status tweet: tweets){ System.out.println("tweet:" + tweet); } } catch (TwitterException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }