Java – missing twitter4j authentication credentials
•
Java
I want to use Twitter 4J to tweet in my android app This is my code:
//TWITTER SHARE.
@Click (R.id. img_btn_twitter)
@Background
public void twitterPostWall(){
try {
//Twitter Conf.
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(CONSUMER_KEY)
.setOAuthConsumerSecret(CONSUMER_SECRET)
.setOAuthAccessToken(ACCESS_KEY)
.setOAuthAccessTokenSecret(ACCESS_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
try {
RequestToken requestToken = twitter.getOAuthRequestToken();
Log.e("Request token: ","" + requestToken.getToken());
Log.e("Request token secret: ","" + requestToken.getTokenSecret());
AccessToken accessToken = null;
}
catch (IllegalStateException ie) {
if (!twitter.getAuthorization().isEnabled()) {
Log.e("OAuth consumer key/secret is not set.","");
}
}
Status status = twitter.updateStatus(postLink);
Log.e("Successfully updated the status to [","" + status.getText() + "].");
}
catch (TwitterException te) {
Log.e("TWEET Failed","");
}
}
I always get this error message from twitter4j: Java Lang.illegalstateexception: missing authentication credentials For more information, see http://twitter4j.org/en/configuration.html. But you can see that I'm using the builder to set my key Can someone help me solve it? thank you.
Solution
The problem is the following lines
TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = new TwitterFactory().getInstance();
You are passing the configuration to a twitterfactory instance and using another twitterfactory instance to get a twitter instance
So you get Java Lang.illegalstateexception: missing authentication credentials
I suggest you modify your code as follows:
//Twitter Conf.
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(CONSUMER_KEY)
.setOAuthConsumerSecret(CONSUMER_SECRET)
.setOAuthAccessToken(ACCESS_KEY)
.setOAuthAccessTokenSecret(ACCESS_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
And use this twitter instance It will work
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
