Reputation: 1004
I want to access the Public streaming API sample using Twitter4J. I have created an Application on Twitter and generated the relevant keys and tokens (using the Twitter portal).
But it keeps failing authentication.
Here is the code according to the documentation. A lot of the existing forum posts are out of date.
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();
The error I get is:
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>
Upvotes: 1
Views: 1272
Reputation: 834
This is what I did to authenticate and receive the 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();
}
Hope this helps. The key for me was creating the TwitterStreamFactory passing a configuration object to its constructor. That's how it can authenticate.
Upvotes: 2
Reputation: 1039
The reason for 401 error is Authentication Failure. It can happen due to two reasons
Incorrect Credentials in Configuration [Different ways for Configuring credentials are explained here]
Incorrect System Clock [Sync the System clock even thought you feel that the time is correct]
The second reason can be the cause when you are able to Post a tweet, View timeline etc using API and not able to use twitter streaming.[You get 401 when using streaming API only]
Upvotes: 1