Reputation: 125
I'm working on an Android App which allows users to configure Twitter consumer key and secret by themselves (Access Token and Access Token Secret are not configured). I want to verify key and secret like below before showing login page, but it throws an exception:
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(key, secret);
try {
User user = twitter.verifyCredentials(); // this line throws an exception
} catch (Exception e) {
e.printStackTrace();
}
The exception is:
Twitter credential verification fails.java.lang.IllegalStateException: Authentication credentials are missing. See http://twitter4j.org/en/configuration.html for details
Is there a way to verify the consumer Key and Secret without Access Token and Access Token Access?
Upvotes: 0
Views: 1203
Reputation: 9043
The exception message that you encounter is pretty clear:
Authentication credentials are missing.
You need to authenticate correctly first, before trying to access the Twitter API programmatically. Using only setOAuthConsumer(key, secret)
does not set/configure necessary access token information. Both information needs to be provided (Hint: If you're interested in it, this introduction gives you an overview on how the OAuth authentication works).
According to http://twitter4j.org/en/configuration.html you can then initialize the code via a ConfigurationBuilder
instance like so:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("*your-consumer-key*")
.setOAuthConsumerSecret("*your-consumer-secret*")
.setOAuthAccessToken("*your-access-token*")
.setOAuthAccessTokenSecret("*your-access-token-secret*");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
Given valid a AccessToken/AccessTokenSecret, you should then be able to call:
try {
User user = twitter.verifyCredentials();
// ... do further things....
} catch (Exception e) {
e.printStackTrace();
}
Hope it helps.
Upvotes: 6