即使我在配置中添加了使用者密钥,密钥,我也会收到401错误。但是相同的配置文件适用于以下
TwitterFactory twitterFactory = new TwitterFactory(configuration);
Twitter twitterClient = twitterFactory.getInstance();
但不适用于
TwitterStreamFactory twitterStreamFactory = new TwitterStreamFactory(configuration);
TwitterStream twitterStreamClient = twitterStreamFactory.getInstance();
FilterQuery filterQuery = new FilterQuery(followersId);
twitterStreamClient.addListener(statusListener);
twitterStreamClient.filter(filterQuery);
我收到错误
[Twitter Stream consumer-1[Establishing connection]] INFO - 401:Authentication credentials (https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid consumer key/secret, access token/secret, and the system clock is in sync.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Error 401 Unauthorized</title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing '/1.1/statuses/filter.json'. Reason:
<pre> Unauthorized</pre>
</body>
</html>
当我打印System.out.println(twitterStreamClient.getOAuthAccessToken().getTokenSecret());
时,它显示了accessestoken秘密值。我对此很困惑。有人可以帮帮我吗?
答案 0 :(得分:1)
您必须将消费者密钥/秘密和访问令牌/机密放在twitter4j.properties中,并将其放在项目的src包中。之后写
TwitterStream ts = new TwitterStreamFactory().getInstance();
您是否已将lib / twitter4j-stream-3.0.3.jar添加到您的库中?
答案 1 :(得分:1)
我必须运行一个twitter ripper独立的Java应用程序和Tomcat webapp。在没有找到类路径和诸如twitter4j.properties的情况下苦苦挣扎我已经开始提供显式配置对象。
现在很容易在同一个myapp.properties文件或数据库表中维护Twitter4J,OAuth和其他特定于应用程序的参数。在这里,我使用相同的配置创建流式和非流式Twitter源对象。
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(debug)
.setPrettyDebugEnabled(debug)
.setHttpConnectionTimeout(15*1000)
.setHttpReadTimeout(60*1000)
.setIncludeRTsEnabled(config.getBoolean("includeRetweets"))
.setIncludeEntitiesEnabled(config.getBoolean("includeEntities"))
.setOAuthConsumerKey( config.getProperty("auth.key") )
.setOAuthConsumerSecret( config.getProperty("auth.secret") )
.setOAuthAccessToken( config.getProperty("auth.token") )
.setOAuthAccessTokenSecret( config.getProperty("auth.tokenSecret") );
Configuration tfConf = cb.build();
twitter = new TwitterStreamFactory(tfConf).getInstance();
twitterNoStream = new TwitterFactory(tfConf).getInstance();
. . . . .
// later once we are done with twitter, call shutdown
twitter.shutdown();
twitterNoStream.shutdown();