gpt4 book ai didi

Twitter4j : Search for public tweets

转载 作者:行者123 更新时间:2023-12-02 10:39:05 26 4
gpt4 key购买 nike

如何使用 Twitter4j 库在公共(public)推文中进行搜索?

public static void main(String[] args) {
Twitter twitter = new TwitterFactory().getInstance();
try {
Query query = new Query("Cocaine");
QueryResult result;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
}
} while ((query = result.nextQuery()) != null);
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
}

这给了我一个身份验证凭据丢失。错误

最佳答案

您需要在 Twitter 上注册您的应用。仅当您拥有自己的私有(private)帐户时才可以这样做。获得 ConsumerKeyConsumerSecretAccessTokenAccessTokenSecret 后(在您注册应用程序时将向您提供它们) ),最简单的解决方案是将代码更改为以下示例:

public static void main(String[] args) {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("yourConsumeKey")
.setOAuthConsumerSecret("yourConsumerSecret")
.setOAuthAccessToken("yourAccessToken")
.setOAuthAccessTokenSecret("yourTokenSecret");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
try {
Query query = new Query("query");
QueryResult result;
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
}

System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
}

关于Twitter4j : Search for public tweets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13545936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com