Search code examples
javatwittertwitter-oauthtwitter4j

401 Error when creating twitterstream


even though I have added the consumer key ,secret key in the configuration i get the 401 error. But the same configuration file is working good for the following

TwitterFactory twitterFactory = new TwitterFactory(configuration);
Twitter twitterClient = twitterFactory.getInstance();   

but not working for the

 TwitterStreamFactory twitterStreamFactory = new TwitterStreamFactory(configuration);
TwitterStream twitterStreamClient = twitterStreamFactory.getInstance(); 
FilterQuery filterQuery = new FilterQuery(followersId);
    twitterStreamClient.addListener(statusListener);
    twitterStreamClient.filter(filterQuery);  

i got the error as

[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>

When i print the System.out.println(twitterStreamClient.getOAuthAccessToken().getTokenSecret()); it shows the accesstoken secret value. I am confused with this. Anyone can help me?


Solution

  • You must put consumer key/secret and acces token/secret in twitter4j.properties and place it in src package in your project. After that write

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

    have you added lib/twitter4j-stream-3.0.3.jar to your library?