Reputation: 121
I am trying to use Twitter4J and for some reason I am not able to properly authenticate.I am getting an error that something is wrong with my authentication. I have tried all the different methods available online but none of them seemed to be working. Could someone tell me what am I doing wrong here? accessToken, accessSecret,etc. all seems to be correct.
Twitter twitter = new TwitterFactory().getInstance();
AccessToken a = new AccessToken(accessToken, accessSecret);
twitter.setOAuthConsumer(consumerKey, consumerSecret);
twitter.setOAuthAccessToken(a);
//twitter.verifyCredentials();
System.out.println(twitter.getScreenName());
Upvotes: 0
Views: 291
Reputation: 9456
First you need to get consumer secret/token and access token/secret from https://dev.twitter.com/
Try this code.
String consumerKey = "yourconsumerKey ";
String consumerSecret = "yourconsumerSecret";
String accessToken = "yourAccessToken";
String accessSecret = "yourAccessSecret";
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(consumerKey)
.setOAuthConsumerSecret(consumerSecret)
.setOAuthAccessToken(accessToken)
.setOAuthAccessTokenSecret(accessSecret);
try
{
TwitterFactory factory = new TwitterFactory(cb.build());
Twitter twitter = factory.getInstance();
System.out.println(twitter.getScreenName());
}catch (TwitterException te) {
te.printStackTrace();
System.exit(-1);
}
Upvotes: 1