1

即使我在配置中添加了消费者密钥、密钥,我也会收到 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());它时,它会显示访问令牌的秘密值。我对此感到困惑。任何人都可以帮助我吗?

4

2 回答 2

1

您必须将使用者密钥/秘密和访问令牌/秘密放在 twitter4j.properties 中,并将其放在项目的 src 包中。之后写

TwitterStream ts = new TwitterStreamFactory().getInstance();

您是否已将 lib/twitter4j-stream-3.0.3.jar 添加到您的库中?

于 2013-06-14T16:58:09.503 回答
1

我必须在 Tomcat webapp 中运行一个 twitter ripper 独立 Java 应用程序。挣扎于类路径和找不到 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();
于 2014-06-26T12:38:59.320 回答