我正在使用twitter流API从用户那里获取tweet。我有用户Id列表我如何从TwitterStream API获得推文列表。我分析了以下样本
public class TwitterStreamAPI {
public static void main(String[] args) {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey("xxxx");
cb.setOAuthConsumerSecret("xxx");
cb.setOAuthAccessToken("xxx");
cb.setOAuthAccessTokenSecret("xxx");
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
UserStreamListener listener = new UserStreamListener() {
@Override
public void onStatus(Status status) {
System.out.println("onStatus @" + status.getUser().getScreenName() + " - " + status.getText());
}
@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
//System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
@Override
public void onDeletionNotice(long directMessageId, long userId) {
// System.out.println("Got a direct message deletion notice id:" + directMessageId);
}
@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
// System.out.println("Got a track limitation notice:" + numberOfLimitedStatuses);
}
@Override
public void onScrubGeo(long userId, long upToStatusId) {
// System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
@Override
public void onStallWarning(StallWarning warning) {
// System.out.println("Got stall warning:" + warning);
}
@Override
public void onFriendList(long[] friendIds) {
/*System.out.print("onFriendList");
for (long friendId : friendIds) {
System.out.print(" " + friendId);
}
System.out.println();*/
}
@Override
public void onFavorite(User source, User target, Status favoritedStatus) {
/* System.out.println("onFavorite source:@"
+ source.getScreenName() + " target:@"
+ target.getScreenName() + " @"
+ favoritedStatus.getUser().getScreenName() + " - "
+ favoritedStatus.getText());*/
}
@Override
public void onUnfavorite(User source, User target, Status unfavoritedStatus) {
/*System.out.println("onUnFavorite source:@"
+ source.getScreenName() + " target:@"
+ target.getScreenName() + " @"
+ unfavoritedStatus.getUser().getScreenName()
+ " - " + unfavoritedStatus.getText());*/
}
@Override
public void onFollow(User source, User followedUser) {
/*System.out.println("onFollow source:@"
+ source.getScreenName() + " target:@"
+ followedUser.getScreenName());*/
}
@Override
public void onDirectMessage(DirectMessage directMessage) {
/* System.out.println("onDirectMessage text:"
+ directMessage.getText());*/
}
@Override
public void onUserListMemberAddition(User addedMember, User listOwner, UserList list) {
/*System.out.println("onUserListMemberAddition added member:@"
+ addedMember.getScreenName()
+ " listOwner:@" + listOwner.getScreenName()
+ " list:" + list.getName());*/
}
@Override
public void onUserListMemberDeletion(User deletedMember, User listOwner, UserList list) {
/* System.out.println("onUserListMemberDeleted deleted member:@"
+ deletedMember.getScreenName()
+ " listOwner:@" + listOwner.getScreenName()
+ " list:" + list.getName());*/
}
@Override
public void onUserListSubscription(User subscriber, User listOwner, UserList list) {
/*System.out.println("onUserListSubscribed subscriber:@"
+ subscriber.getScreenName()
+ " listOwner:@" + listOwner.getScreenName()
+ " list:" + list.getName());*/
}
@Override
public void onUserListUnsubscription(User subscriber, User listOwner, UserList list) {
/*System.out.println("onUserListUnsubscribed subscriber:@"
+ subscriber.getScreenName()
+ " listOwner:@" + listOwner.getScreenName()
+ " list:" + list.getName());*/
}
@Override
public void onUserListCreation(User listOwner, UserList list) {
/*System.out.println("onUserListCreated listOwner:@"
+ listOwner.getScreenName()
+ " list:" + list.getName());*/
}
@Override
public void onUserListUpdate(User listOwner, UserList list) {
/*System.out.println("onUserListUpdated listOwner:@"
+ listOwner.getScreenName()
+ " list:" + list.getName());*/
}
@Override
public void onUserListDeletion(User listOwner, UserList list) {
/*System.out.println("onUserListDestroyed listOwner:@"
+ listOwner.getScreenName()
+ " list:" + list.getName());*/
}
@Override
public void onUserProfileUpdate(User updatedUser) {
//System.out.println("onUserProfileUpdated user:@" + updatedUser.getScreenName());
}
@Override
public void onBlock(User source, User blockedUser) {
/*System.out.println("onBlock source:@" + source.getScreenName()
+ " target:@" + blockedUser.getScreenName());*/
}
@Override
public void onUnblock(User source, User unblockedUser) {
/*System.out.println("onUnblock source:@" + source.getScreenName()
+ " target:@" + unblockedUser.getScreenName());*/
}
@Override
public void onException(Exception ex) {
/* ex.printStackTrace();
System.out.println("onException:" + ex.getMessage());*/
}
};
String useIds[] = {"99901654"};
twitterStream.addListener(listener);
twitterStream.user(useIds);
}
}
https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/stream/PrintUserStream.java
但它不打印状态
流媒体api为您提供实时事件,因此,如果在您收听期间没有用户Tweet,您将看不到任何输出。
如果你试图获取用户的历史推文,请考虑使用Twitter#getUserTimeline(screenName)
,例如:
final Twitter twitter = new TwitterFactory().getInstance();
final List<Status> statuses = twitter.getUserTimeline(screenName);
for (Status status : statuses) {
System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
}
有关更多详细信息,请参见此示例。
我在这里已经详细回答了同样的问题。请检查一下。这将向您展示我们如何使用所需的访问令牌读取和更新推特。
简而言之,以下是的步骤
- 在上装入推特应用程序https://apps.twitter.com/app/new并获取您的消费者密钥。(如果你发现了什么地方,请关注上面提到的博客。)
- 使用Twitter4j API获取授权URL,点击它并获取PIN。(下面提到了所需的代码供您参考)
- 输入引脚并获取通道(参见以下代码)
- 这就是我们现在可以阅读或更新推特订阅源的全部内容。(基于您在创建twitter应用程序时设置的访问级别)
请在此处查看示例代码。https://stackoverflow.com/a/40691881/2405670