Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

twitter4j: 401 authentication credentials missing or incorrect

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
like image 844
jcfrei Avatar asked May 03 '11 11:05

jcfrei


3 Answers

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.

like image 104
Kishan Bheemajiyani Avatar answered Nov 13 '22 06:11

Kishan Bheemajiyani


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.

like image 25
Punit Raizada Avatar answered Nov 13 '22 04:11

Punit Raizada


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.

like image 1
Etienne Dufresne Avatar answered Nov 13 '22 04:11

Etienne Dufresne