gpt4 book ai didi

java - 如何从 Twitter 流获取样本并打印到屏幕

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:49 25 4
gpt4 key购买 nike

我正在尝试使用 twitter4j 库从 Twitter 流中获取随机样本并打印推文的用户名和文本。

OAuth 而言,我不确定我是否理解正确的术语。这是我的帐户需要获取的东西还是仅在访问其他私有(private)帐户时才需要获取的东西?

这是我的代码:

package tweet1;

import java.io.IOException;

import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.auth.BasicAuthorization;

public class Twit4jEx1 {

/**
* @param args
*/
// TODO Auto-generated method stub
public static void main(String[] args) throws TwitterException, IOException{
// BasicAuthorization ba1 = new BasicAuthorization("userName", "pword");
ConfigurationBuilder cb = new ConfigurationBuilder();

StatusListener listener = new StatusListener(){
public void onStatus(Status status) {
System.out.println(status.getUser().getName() + " : " + status.getText());
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
public void onException(Exception ex) {
ex.printStackTrace();
}
@Override
public void onScrubGeo(long arg0, long arg1) {
// TODO Auto-generated method stub

}
};
TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
// sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
twitterStream.sample();
}
}

ConfigurationBuilder类

package tweet1;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
//import twitter4j.conf.ConfigurationBuilder;

public class ConfigurationBuilder {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("***")
.setOAuthConsumerSecret("***")
.setOAuthAccessToken("***")
.setOAuthAccessTokenSecret("***");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
}

当我运行 Twit4jEx1 时,我得到以下信息:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Syntax error on token(s), misplaced construct(s)
Syntax error on token "setDebugEnabled", = expected after this token

at tweet1.ConfigurationBuilder.<init>(ConfigurationBuilder.java:9)
at tweet1.Twit4jEx1.main(Twit4jEx1.java:21)

有什么建议吗?

最佳答案

您的 ConfigurationBuilder 类正在尝试在初始化 block 中而不是在构造函数或方法中运行代码。

我认为你的意思是:

public class MyConfigurationBuilder {
private ConfigurationBuilder cb;
private Twitter twitter;

public MyConfigurationBuilder() {
cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("***")
.setOAuthConsumerSecret("***")
.setOAuthAccessToken("***")
.setOAuthAccessTokenSecret("***");
TwitterFactory tf = new TwitterFactory(cb.build());
twitter = tf.getInstance();
}
}

然后你可以添加适当的 setter/getter 或其他东西。确保在主函数中使用 MyConfigurationBuilder。

关于java - 如何从 Twitter 流获取样本并打印到屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12097247/

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