Twitter4j最新版本移除setTweetModeExtended后,如何获取完整推文内容?
Hey there, I’ve run into this exact issue with the newer Twitter4j versions too—since they nixed the setTweetModeExtended() method, we need to work directly with the underlying API parameters that it used to handle for us. Here’s how to get full tweet text now:
Option 1: Global Configuration (Applies to All Requests)
If you want every tweet fetch request to return full text by default, add the tweet_mode=extended parameter globally in your configuration builder. This replicates the old setTweetModeExtended(true) behavior:
ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey("YOUR_CONSUMER_KEY") .setOAuthConsumerSecret("YOUR_CONSUMER_SECRET") .setOAuthAccessToken("YOUR_ACCESS_TOKEN") .setOAuthAccessTokenSecret("YOUR_ACCESS_TOKEN_SECRET") .setHttpParameter("tweet_mode", "extended"); // This replaces the old method Twitter twitter = new TwitterFactory(cb.build()).getInstance(); Status status = twitter.showStatus(123456789L); // Safely retrieve full text—fallback to regular text if extended data isn't available String fullTweetText = status.getExtendedTweet() != null ? status.getExtendedTweet().getFullText() : status.getText();
Option 2: Per-Request Parameter (Only for Specific Calls)
If you only need full text for certain requests (not all), pass the tweet_mode=extended parameter directly when fetching the tweet:
ConfigurationBuilder cb = new ConfigurationBuilder(); // Basic OAuth setup without global tweet mode cb.setOAuthConsumerKey("YOUR_CONSUMER_KEY") .setOAuthConsumerSecret("YOUR_CONSUMER_SECRET") .setOAuthAccessToken("YOUR_ACCESS_TOKEN") .setOAuthAccessTokenSecret("YOUR_ACCESS_TOKEN_SECRET"); Twitter twitter = new TwitterFactory(cb.build()).getInstance(); // Create a parameter map for the specific request Map<String, String> requestParams = new HashMap<>(); requestParams.put("tweet_mode", "extended"); Status status = twitter.showStatus(123456789L, requestParams); // Same safe retrieval logic String fullTweetText = status.getExtendedTweet() != null ? status.getExtendedTweet().getFullText() : status.getText();
Key Notes
- Make sure your Twitter4j version supports the
getExtendedTweet()method—this was added to replace the old tweet mode configuration, so you’ll need a relatively recent release. - For retweeted tweets, if you want the original tweet’s full text instead of the retweet wrapper, use
status.getRetweetedStatus().getExtendedTweet().getFullText()(always check ifstatus.isRetweet()is true first to avoid null pointers!).
内容的提问来源于stack exchange,提问作者Andrei Stanciu
相关产品推荐
相关产品推荐

