twitter4j - get tweets by ID

后端 未结 2 983
一生所求
一生所求 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: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
        }
    

提交回复
热议问题