2

我正在尝试使用具有单一身份验证的不同 TwitterStream 对象执行不同的线程:

public class Support {
    private static final String accessToken = "xxxxxxx";
    private static final String accessTokenSecret = "xxxxxxx";
    public static final AccessToken token = new AccessToken(accessToken, accessTokenSecret); 
    private static final String consumerKey = "xxxxxxx";
    private static final String consumerSecret = "xxxxxxx";
    private static final Configuration conf = new ConfigurationBuilder().setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret).build(); 
    public static TwitterStreamFactory factory = new TwitterStreamFactory(conf); 
}

在我做的每个线程中:

public MyThread1(){
   this.twitterStream = Support.factory.getInstance(Support.token);
}
public void run(){
        StatusListener listener = ... ;
        twitterStream.addListener(listener);
        FilterQuery fq = new FilterQuery();
        fq.track(new String[]{"hashtag1","hashtag2"});
        twitterStream.filter(fq);
    }

public MyThread2(){
   this.twitterStream = Support.factory.getInstance(Support.token);
}
public void run(){
        StatusListener listener = ... ;
        twitterStream.addListener(listener);
        FilterQuery fq = new FilterQuery();
        fq.track(new String[]{"hashtag3","hashtag4"});
        twitterStream.filter(fq);
    }

但它给了我身份验证错误.. 同一身份验证的多个请求。我该如何解决?

4

1 回答 1

1

这是我的做法:

public class MyTwitterApp implements {

private Twitter twitter;
private Query query;

public MyTwitterApp (){
    twitter = TwitterFactory.getSingleton();
}

public static void main(String[] args) {
    MyTwitterApp twitterApp  = new MyTwitterApp();
    twitterApp.getStreamingTweets();

}

public void getStreamingTweets(){
    StatusListener listener = new StatusListener(){
        public void onStatus(Status status) {
                handleStatus(status);
        }
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
        public void onException(Exception ex) {ex.printStackTrace(); }
        public void onScrubGeo(long arg0, long arg1) {}
        public void onStallWarning(StallWarning arg0) {}
    };
    twitter.addListener(listener);
    FilterQuery fq = new FilterQuery();
    fq.count(0);
    fq.track(new String[]{"#MyHashTag"});
    twitter.filter(fq);
}

protected void handleStatus(Status tweet) {
    if(tweet.isRetweet()){
        return;
    }
    if(isMyHashTagTweet(tweet)){
        //do something with tweet here      
    }       
}

private boolean isMyHashTagTweet(Status tweet) {
    HashtagEntity[] htes = tweet.getHashtagEntities();
    for(HashtagEntity hte : htes){
        if(hte.getText().equalsIgnoreCase("myhashtag")) {
            return true;
        }
    }
    return false;
}
}

每个线程都将包含类似这样的内容。

twitter = TwitterFactory.getSingleton();

将确保您每次都重用相同的连接。

twitter.addListener(listener);

将添加一个侦听器,以便您将被回调到该线程(但您将从添加的每个查询中被回调)

twitter.filter(fq);

将添加一个新的搜索查询来关注。

isMyHashTagTweet(tweet)

将检查以确保您的 twitterStream 中所有查询返回的推文与您当前的线程相关

于 2013-02-21T12:10:52.193 回答