【问题标题】:Accessing the Twitter Stream API with Twitter4J and OAuth使用 Twitter4J 和 OAuth 访问 Twitter Stream API
【发布时间】:2014-03-09 20:48:42
【问题描述】:

我想使用 Twitter4J 访问公共流 API 示例。 我在 Twitter 上创建了一个应用程序并生成了相关的密钥和令牌(使用 Twitter 门户)。

但它总是无法通过身份验证。

这是根据文档的代码。许多现有的论坛帖子已过时。

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey("XXXXX");
cb.setOAuthConsumerSecret("XXXXXXXX");
cb.setOAuthAccessToken("xxxxxxxx-xxxxxxxxxxxxxxxxxxxxxx");
cb.setOAuthAccessTokenSecret("XXXXXXXXXXXXXXXXXXXXXXX");   

OAuthAuthorization auth = new OAuthAuthorization(cb.build());
TwitterStreamFactory factory = new TwitterStreamFactory();
this.twitterStream = factory.getInstance(auth);
this.twitterStream.addListener(listener);
this.twitterStream.sample();

我得到的错误是:

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/sample.json?stall_warnings=true'. Reason:
<pre>    Unauthorized</pre>
</body>
</html>

【问题讨论】:

  • 我不记得了,因为那是很久以前的事了!最后,我不得不手动建立连接并在没有库的情况下自己打开流。

标签: java twitter oauth twitter4j


【解决方案1】:

401 错误的原因是身份验证失败。发生这种情况可能有两个原因

  • Incorrect Credentials in Configuration [解释了配置凭据的不同方法here]

  • 不正确的系统时钟 [同步系统时钟,即使你觉得时间是正确的]

第二个原因可能是您能够使用 API 发布推文、查看时间线等但无法使用 twitter 流式传输。[仅使用流式传输 API 时会出现 401]

【讨论】:

  • 系统时钟?在操作系统上?为什么这很重要?
【解决方案2】:

这是我为验证和接收 twitterstream 所做的:

private static  void listen() throws InterruptedException,TwitterException{

    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setDebugEnabled(true)
        .setOAuthConsumerKey("*************")
        .setOAuthConsumerSecret("******************")
        .setOAuthAccessToken("*******************")
        .setOAuthAccessTokenSecret("************");

    Configuration configuration= configurationBuilder.build();
    TwitterStream twitterStream = new TwitterStreamFactory(configuration).getInstance();

    twitterStream.addListener(new twitter4j.StatusListener() {

        public void onStatus(Status status) {
            System.out.println("Status: {}"+ status);
        }

        public void onException(Exception ex) {
            System.out.println("Error callback"+ ex);
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {

        }

        public void onTrackLimitationNotice(int i) {

        }

        public void onScrubGeo(long l, long l1) {

        }

        public void onStallWarning(StallWarning stallWarning) {

        }

    });
    twitterStream.sample();
    TimeUnit.SECONDS.sleep(10);
    twitterStream.shutdown();
}

希望这会有所帮助。对我来说,关键是创建 TwitterStreamFactory,将配置对象传递给它的构造函数。这就是它可以进行身份​​验证的方式。

【讨论】:

    猜你喜欢
    • 2013-07-04
    • 2011-11-15
    • 2014-12-23
    • 2019-01-15
    • 2011-05-03
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    相关资源
    最近更新 更多