2013-05-30 25 views
1

即使我已經加入消費者鍵時,在配置密鑰我得到的401錯誤。但是,同樣的配置文件是工作好以下401錯誤創建twitterstream

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());它顯示了祕密的祕密價值。我對此感到困惑。任何人都可以幫助我?

回答

1

你必須把消費者的鍵/祕密和twitter4j.properties存取權限令牌/祕密並將其放置在SRC包在你的項目。之後寫

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

你添加的lib/twitter4j流-3.0.3.jar到您的圖書館?

1

我不得不跑一個嘰嘰喳喳鬆土獨立的Java應用程序和Tomcat Web應用程序內。掙扎着類路徑和未找到twitter4j.properties我已經開始給出顯式配置對象。

它現在易於維護Twitter4J,OAuth的和其他應用程序特定的參數在同一個myapp.properties文件或數據庫表。在這裏,我使用相同的配置創建流媒體和非流媒體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();