twitter4j - 通过ID获取推文

时间:2012-12-01 17:07:26

标签: twitter4j

如果我有推文ID和用户ID,我怎样才能收到推文?我有一个包含以下行的文件:

userID  tweetID

我想我应该过去:

Query query = new Query("huh ?");
QueryResult result = twitter.search(query);
List<Status> tweets = result.getTweets();

但我不知道如何拼写query

由于

2 个答案:

答案 0 :(得分:14)

那不是搜索电话。这条推文显然被称为Status,而通过ID检索的推文是:

    final Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);
    AccessToken accessToken = new AccessToken(TWITTER_TOKEN,
            TWITTER_TOKEN_SECRET);
    twitter.setOAuthAccessToken(accessToken);
    try {
        Status status = twitter.showStatus(Long.parseLong(tweetID));
        if (status == null) { // 
            // don't know if needed - T4J docs are very bad
        } else {
            System.out.println("@" + status.getUser().getScreenName()
                        + " - " + status.getText());
        }
    } catch (TwitterException e) {
        System.err.print("Failed to search tweets: " + e.getMessage());
        // e.printStackTrace();
        // DON'T KNOW IF THIS IS THROWN WHEN ID IS INVALID
    }

答案 1 :(得分:0)

接受的答案不再有效。根据{{​​3}}中的答案,代码应更改为以下内容:

    String consumerKey = xxxxxxx,
            consumerSecret = xxxxxxx,
            twitterAccessToken = xxxxxxx,
            twitterAccessTokenSecret = xxxxxxx,
            Tweet_ID = xxxxxxx;

    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(consumerKey);
    builder.setOAuthConsumerSecret(consumerSecret);
    Configuration configuration = builder.build();
    TwitterFactory factory = new TwitterFactory(configuration);
    final Twitter twitter = factory.getInstance();

    //twitter.setOAuthConsumer(consumerKey, consumerSecret);
    AccessToken accessToken = new AccessToken(twitterAccessToken, twitterAccessTokenSecret);
    twitter.setOAuthAccessToken(accessToken);
    try {
        Status status = twitter.showStatus(Long.parseLong(Tweet_ID));
        if (status == null) { //
            // don't know if needed - T4J docs are very bad
        } else {
            System.out.println("@" + status.getUser().getScreenName()
                    + " - " + status.getText());
        }
    } catch (
            TwitterException e) {
        System.err.print("Failed to search tweets: " + e.getMessage());
        // e.printStackTrace();
        // DON'T KNOW IF THIS IS THROWN WHEN ID IS INVALID
    }