Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

java - Twitter4j authentication credentials are missing

I would like to make a tweet with Twitter4j in my Android app. Here 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: Authentication credentials are missing. See http://twitter4j.org/en/configuration.html for the detail. But as you can see I'm using builder to set my key. Can someone help me to fix it please? thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Problem is following lines.

TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = new TwitterFactory().getInstance();

You are passing the configuration to one TwitterFactory instance and using another TwitterFactory instance to get the Twitter instance.

Hence, You are getting java.lang.IllegalStateException: Authentication credentials are missing

I suggest you to 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...