1
我試圖使用twitter4j
庫從twitter流中獲取隨機樣本並打印推文的用戶名和文本。如何從twitter流中獲取樣本並將其打印到屏幕
至於OAuth
,我不確定我是否理解正確的術語。這是我需要爲我的帳戶獲得的東西,還是僅在訪問其他私人帳戶時才需要這些東西?
這裏是我的代碼:
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)
有什麼建議?
你看過這個 - > http://twitter4j.org/en/code-examples.html – AnujKu
而這 - > http://stackoverflow.com/questions/7547470/twitter4j-twitterstream-doesnt-get-除此之外 - > http://twitter4j.org/en/javadoc/index.html – AnujKu
@AnujKulkarni:是的,這就是我從中獲得代碼的地方。 – screechOwl