Reputation: 31
I've got a problem with trying to authenticate to Twitter using twitter4j. I've tried this out and yet it is still not working.
Here's my code, any help would be greatly appreciated.
Thanks.
public class SpeedDemon {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws TwitterException {
// Setup for Snake Charmer
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("CONSUMER_KEY")
.setOAuthConsumerSecret("CONSUMER_SECRET")
.setOAuthAccessToken("OAUTH_ACCESS")
.setOAuthAccessTokenSecret("OAUTH_SECRET");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
// Gets timeline.
Twitter twit = TwitterFactory.getSingleton();
List<Status> statuses = twit.getHomeTimeline();
System.out.println("Showing home timeline.");
for (Status status : statuses) {
System.out.println(status.getUser().getName() + ":" +
status.getText());
}
}
}
EDIT: The following error happens at compilation:
Exception in thread "main" java.lang.IllegalStateException: Authentication credentials are missing. See http://twitter4j.org/en/configuration.html for details
at twitter4j.TwitterBaseImpl.ensureAuthorizationEnabled(TwitterBaseImpl.java:215)
at twitter4j.TwitterImpl.get(TwitterImpl.java:1784)
at twitter4j.TwitterImpl.getHomeTimeline(TwitterImpl.java:105)
at speeddemon.SpeedDemon.main(SpeedDemon.java:30)
C:\Users\Kevin\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Upvotes: 0
Views: 1229
Reputation: 737
It looks like you construct a Twitter instance twice, once with your constructed TwitterFactory and once with the Singleton (which I suspect doesn't have auth setup).
You then use the second Twitter instance (created with the unauthenticated factory) to make your queries.
Try using twitter.getHomeTimeline()
rather than twit.getHomeTimeline()
Upvotes: 1