Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter4J verify Credentials and catching error

I have the following code on google appengine to test if twitter credentials are valid using twitter4j

public void doGet(HttpServletRequest req, HttpServletResponse resp) {
    String Test = "Testing Twitter4J";
    try {

        ConfigurationBuilder confbuilder  = new ConfigurationBuilder();
        confbuilder.setOAuthAccessToken("A") 
        .setOAuthAccessTokenSecret("B") 
        .setOAuthConsumerKey("C") 
        .setOAuthConsumerSecret("D"); 
        Twitter twitter = new TwitterFactory(confbuilder.build()).getInstance(); 

        //Status status = twitter.updateStatus("Working lunch today");
        User user = twitter.verifyCredentials();
        Test = "Successfully updated the status to [" + user.getScreenName() + "].";

    } catch (TwitterException e) {
        Test =  "no";
    }
    try {
        resp.getOutputStream().write(Test.getBytes());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return;
}

If the credentials are valid it works as expected. If I revoke access so the credentials are not valid I get an app engine error:

Uncaught exception from servlet
twitter4j.TwitterRuntimeException: 401:Authentication credentials (https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid consumer key/secret, access token/secret, and the system clock is in sync.
message - Invalid or expired token
code - 89

Relevant discussions can be found on the Internet at:
    http://www.google.co.jp/search?q=93f2523c or
    http://www.google.co.jp/search?q=8b74a4e3
TwitterException{exceptionCode=[93f2523c-8b74a4e3], statusCode=401, message=Invalid or expired token, code=89, retryAfter=-1, rateLimitStatus=null, version=3.0.3}

from the "User user = twitter.verifyCredentials();" line. I was expecting the TwitterException to be thrown from the javadoc at: http://twitter4j.org/javadoc/twitter4j/api/UsersResources.html#verifyCredentials() but it seems I get a TwitterRuntimeException. Why? I can't find any notes of a TwitterRuntimeException in the javadocs.

like image 517
Michael Avatar asked Nov 12 '22 11:11

Michael


1 Answers

For me the solution was the following:

  • Go to your twitter app
  • Select the settings tab
  • Under Application Type - Access - Select the last option Read,Write and Access direct messages

If this doesn't solve the problem try adding Callback URL. You can put any url here it will be fine.

like image 137
Darko Petkovski Avatar answered Nov 15 '22 07:11

Darko Petkovski