您好我是Twitter的新手,从Python回到Java。
我正在尝试使用oauth进行身份验证,并使用twitter4j实时抓取推文。
所以,当我运行下面的代码列表它看起来没问题,但我不确定我是否正在连接!
此外,我不确定如何收听流并提取推文。
到目前为止我的代码:
package twitterfordatalake;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import twitter4j.StallWarning;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;
import static twitterfordatalake.TwitterForDataLake.props;
//Testing
import twitter4j.TwitterFactory;
import twitter4j.Twitter;
import twitter4j.Query;
import twitter4j.QueryResult;
/**
*
* @author crigano
*/
public class TwitterForDataLake
{
static Properties props = new Properties();
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws TwitterException, IOException
{
// TODO code application logic here
System.out.println("Hellow World");
TwitterForDataLake ts = new TwitterForDataLake();
ts.TwitterScraper();
}
public void TwitterScraper() throws IOException
{
// read the properties file options
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(
props.getProperty("oauth.consumerKey"))
.setOAuthConsumerSecret(
props.getProperty("oauth.consumerSecret"))
.setOAuthAccessToken(
props.getProperty("oauth.accessToken"))
.setOAuthAccessTokenSecret(
props.getProperty("oauth.accessTokenSecret"));
TwitterStream twitterStream = new TwitterStreamFactory(cb.build())
.getInstance();
}
}
I used the following initialization twitter4j.properties file:
debug=true
oauth.consumerKey=AAAA
oauth.consumerSecret=BBBB
oauth.accessToken=CCCC
oauth.accessTokenSecret=DDDD
This is based on what I got from twitter:
AAAA
API secret BBBB
Access level Read, write, and direct messages (modify app permissions)
Owner me
Owner ID 66666
Your access token
This access token can be used to make API requests on your own account's behalf. Do not share your access token secret with anyone.
Access token CCCC
Access token secret DDDD
Access level Read, write, and direct messages
Owner me
Owner ID 6666
我非常感谢有关调试誓言的一些建议。
我非常感谢一小时内英语推文听众的示例代码。
非常感谢,
克里斯
答案 0 :(得分:0)
我粘贴在java中运行的代码下面。您可以将它用于同一目的:
//首先,我们需要一个带有有效身份验证令牌的Twitter对象。
public static ConfigurationBuilder confBuilder = null;
public static Configuration config = null;
public static Twitter twitterObj = null;
confBuilder = new twitter4j.conf.ConfigurationBuilder();
confBuilder.setDebugEnabled(true);
confBuilder.setOAuthConsumerKey("consumerKey");
confBuilder.setOAuthConsumerSecret("consumerSecret");
confBuilder.setOAuthAccessToken("accessToken");
confBuilder.setOAuthAccessTokenSecret("accessTokenSecret");
config = confBuilder.build();
twitter4j.TwitterFactory tf = new twitter4j.TwitterFactory(config);
twitterObj = tf.getInstance(); //Here, finally we got the twitter object.
//稍后,我们需要使用流功能,也是这样的:
TwitterStream twitterStream = new TwitterStreamFactory(config).getInstance();
StatusListener statusListener = new StatusListener() {
private int count = 0;
private long originalTweetId = 0;
@Override
public void onStatus(Status status) {
// status object here is the tweet object that caught by the stream. You can do whatever you want with it.
}
@Override
public void onDeletionNotice(StatusDeletionNotice sdn) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onTrackLimitationNotice(int i) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onScrubGeo(long l, long l1) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onStallWarning(StallWarning sw) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void onException(Exception ex) {
logWriter.WriteErrorLog(ex, "onException()");
}
};
FilterQuery fq = new FilterQuery();
String keywords[] = {keyword};
fq.track(keywords);
twitterStream.addListener(statusListener);
twitterStream.filter(fq);
//我希望这就是你要找的东西。祝你好运!