twitter4j: 401 authentication credentials missing or incorrect

15,915

Solution 1

I had the same issue. The consumer key and consumer secret I was using were correct and my machine was in sync. The problem was that i did not set the callback URL when i was registering my application on dev.twitter.com. Once i set that, everything just worked.

Solution 2

There is problem with Consumer key and Token. follow the steps.

login to https://dev.twitter.com/ and Goto My application means your respected application. and goto settings and Change access permission Read-only to Read, write, and direct messages

and update data and if it is not updated then recreate token and update data and your program too. and run the program and make sure you are having all jar file of that in your lib folder. and problem will be solved.

Solution 3

I thought I would share this in case it helps someone Googling for this exception message.

While testing Twitter authentication for a JSF application, I tried the following use case:

Open the Twitter authentication window. Enter proper credentials and sign in.

It worked. So then I tried:

Open the Twitter authentication window. Close the Twitter authentication window. Open the Twitter authentication window again. Enter proper credentials and hit Sign in.

The latter always resulted in a 401 authentication credentials missing or incorrect exception.

My problem was that the server was restarting the Twitter authentication process but the value of RequestToken.getAuthenticationURL() was not being properly passed back to the UI and caused it to invoke the Twitter API with the old URL.

In brief, ensure the authentication URL used by the client matches the one the server is expecting.

Share:
15,915

Related videos on Youtube

jcfrei
Author by

jcfrei

software and finance guy

Updated on June 01, 2022

Comments

  • jcfrei
    jcfrei almost 2 years

    I'm currently trying to get the userTimeline of a bunch of users. This has worked for me in the past, but now doesn't work anymore. I'm using twitter4j and using oauth. I've registered 2 applications on my account so far, which both should be able to access the twitter-api. However since yesterday I'm getting a 401 - authentication redentials missing or incorrect (tried both applications). Did twitter make any recent changes to the API? Should I setup a new twitter account? Is it an appropriate way to create an application and allow it to access your own account?

    This is the code:

    ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
          .setOAuthConsumerKey("q...w")
          .setOAuthConsumerSecret("R...o")
          .setOAuthAccessToken("2...7")
          .setOAuthAccessTokenSecret("O....8");
    
      TwitterFactory tf = new TwitterFactory(cb.build());
    
        Twitter twitter = tf.getInstance();
      for ( Map.Entry<Long, String> entry : tmls.entrySet()) {
    
            long uid = entry.getKey();
    
            Paging paging = new Paging(1, count);
    
            ResponseList<Status> rls;
    
            try {
                rls = twitter.getUserTimeline(uid, paging);
    
                for ( int j = 0 ; j < rls.size() ; j++ ) {
    
                    if (false == ltindex.contains(uid)) {
    
    
                        alsa.add(new String[] {  String.valueOf( rls.get(j).getId() ) , String.valueOf( uid ), rls.get(j).getText() , "", entry.getValue(), String.valueOf( rls.get(j).getCreatedAt().getTime() )  });
    
                    }
    
                }
    
    
            } catch (TwitterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
      }
    

    the error:

      401:Authentication credentials (http://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid conumer key/secret, access token/secret, and the system clock in in sync.
      error - Not authorized
      request - /1/statuses/user_timeline.json?user_id=14221532&include_rts=true&include_entities=false&count=10&page=1
      Relevant discussions can be on the Internet at:
        http://www.google.co.jp/search?q=ced778ef or
        http://www.google.co.jp/search?q=10a1ea9d
      TwitterException{exceptionCode=[ced778ef-10a1ea9d], statusCode=401, retryAfter=0, rateLimitStatus=RateLimitStatusJSONImpl{remainingHits=273, hourlyLimit=350, resetTimeInSeconds=1304422, secondsUntilReset=845, resetTime=Tue May 03 11:36:48 UTC 2011}, version=2.2.3-SNAPSHOT(build: ddf24547632bf3a28b899e3d75b110de43f71c0f)}
        at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:189)
        at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
        at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:85)
        at twitter4j.TwitterImpl.get(TwitterImpl.java:1738)
        at twitter4j.TwitterImpl.getUserTimeline(TwitterImpl.java:246)
        at getExpertTweets.getSpecificOpinions(getExpertTweets.java:89)
        at classifyTweet.main(classifyTweet.java:57)
      root@se1:~# sudo ntpdate pool.ntp.org
    
    • jcfrei
      jcfrei about 13 years
      yes, I've updated it several times with ntpdate pool.ntp.org
    • D-Nice
      D-Nice almost 13 years
      Yea, it almost certainly means that the timeline is private and you haven't been granted permission to follow the user in question.
  • mn0102
    mn0102 over 10 years
    how to set the callback_url, it says invalid url. The format I used anything://twitter
  • Abandoned Cart
    Abandoned Cart over 5 years
    Issue still happens 5 years later. Unfortunately, this has never been the solution.