简体   繁体   中英

Twitter4j: OAuth Consumer Key/Secret Not Set

I've just started using Twitter4j and am trying to get my first application working. I've registered my application on the Twitter applications page and entered OAuth information into the twitter4j.properties file. However, every time I try to run the application I get an error message saying 'OAuth Consumer Key/Secret Not Set'. I've included the code with this message.

Any help would be greatly appreciated.

Thanks in advance, Aonghus

import java.util.logging.Logger;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;


public class test {
    private final Logger logger = Logger.getLogger(test.class.getName());
    public static void main(String[] args) {
        new test().publish();
    }

    private void publish(){

        String message="Twitter application using Java http://www.java-tutorial.ch/architecture/twitter-with-java-tutorial";

        try {
            Twitter twitter = new TwitterFactory().getInstance();
            try {
                RequestToken requestToken = twitter.getOAuthRequestToken();
                AccessToken accessToken = null;
                while (null == accessToken) {
                    logger.fine("Open the following URL and grant access to your account:");
                    logger.fine(requestToken.getAuthorizationURL());
                    try {
                        accessToken = twitter.getOAuthAccessToken(requestToken);
                    } 
                    catch (TwitterException te) {
                        if (401 == te.getStatusCode()) {
                            logger.severe("Unable to get the access token.");
                        } else {
                            te.printStackTrace();
                        }
                    }
                }
                logger.info("Got access token.");
                logger.info("Access token: " + accessToken.getToken());
                logger.info("Access token secret: " + accessToken.getTokenSecret());
            } 
            catch (IllegalStateException ie) {
                // access token is already available, or consumer key/secret is not set.
                if (!twitter.getAuthorization().isEnabled()) {
                    logger.severe("OAuth consumer key/secret is not set.");
                    return;
                }
            }
            Status status = twitter.updateStatus(message);
            logger.info("Successfully updated the status to [" + status.getText() + "].");
        } 
        catch (TwitterException te) {
            te.printStackTrace();
            logger.severe("Failed to get timeline: " + te.getMessage());
        }
    }
}

you need to create a file in the project which will look like that

debug=false
oauth.consumerKey=**************
oauth.consumerSecret=************************************
oauth.accessToken=****************************************
oauth.accessTokenSecret=*******************************

http://twitter4j.org/en/configuration.html

twitter4j.properties is one way.

您需要设置使用者密钥/秘密。您必须从Twitter Developers获得此密钥

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM