muthu
muthu

Reputation: 5461

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?

Upvotes: 1

Views: 724

Answers (2)

Whome
Whome

Reputation: 10400

I had to run one twitter ripper standalone java app and inside Tomcat webapp. Struggling with classpath and whatnot twitter4j.properties not found I've started giving explicit configuration object.

Its now easy to maintain Twitter4J, OAuth and other app specific parameters in a same myapp.properties file or in database table. Here I create streaming and nonstreaming Twitter source object with same configs.

    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();

Upvotes: 1

orbita
orbita

Reputation: 179

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?

Upvotes: 1

Related Questions