twitter4j - get tweets by ID

后端 未结 2 980
一生所求
一生所求 2021-02-10 18:39

How can I get the tweets when I have the tweet ID and the user ID ? I have a file containing lines like :

userID  tweetID

I guess I should go b

相关标签:
2条回答
  • 2021-02-10 19:08

    Well it was no search call. The tweet apparently is called Status and the code to retrieve one by ID is :

        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
        }
    
    0 讨论(0)
  • 2021-02-10 19:23

    The accepted answer is no longer valid. Based on the answer in this page, the code should be changed to the following:

        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
        }
    
    0 讨论(0)
提交回复
热议问题