jcfrei
jcfrei

Reputation: 1809

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

Upvotes: 11

Views: 17324

Answers (7)

adarsh
adarsh

Reputation: 161

Check all your auth credentials are correct a) consumerKey b) consumerSecret c) accessToken d) accessTokenSecret If all the above are correct, It should work.

Another possibility can be when your system is out of sync. Such as if you are running in virtual box after few hours of inactivity your system get out of sync. Please restart the system and try again.

You can validate the dependencies with below build.sbt file

name := "sparkstreamingexamples"
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
  "org.apache.spark" %% "spark-streaming" % "2.0.1",
  "org.apache.bahir" %% "spark-streaming-twitter" % "2.0.1",
"org.twitter4j" % "twitter4j-core" % "4.0.4"
)

Upvotes: 0

Suresh
Suresh

Reputation: 427

I faced the same issue and I fixed, If the device date and time is not correct then this problem will occur..

Upvotes: 0

Ivan
Ivan

Reputation: 3062

I faced this issue as well with correct system time and API Key/Secret.

It turned out that one needs to set Callback URL to https://api.twitter.com/oauth/request_token Once it is set - you will see Twitter Authorize screen.

You can also press "Test OAuth" to test it using OAuth Tool and pass https://api.twitter.com/oauth/request_token as a Request URI for POST request.

Upvotes: 0

Today 02.05.2015 I got the similar error using a twitter4j 3.0.x version.

I got it until I updated twitter4j to version 4.0.0.

  <dependencies>
  <dependency>
      <groupId>org.twitter4j</groupId>
      <artifactId>twitter4j-core</artifactId>
      <version>[4.0,)</version>
  </dependency>
  ...

Upvotes: 0

Etienne Dufresne
Etienne Dufresne

Reputation: 411

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.

Upvotes: 1

Kishan Bheemajiyani
Kishan Bheemajiyani

Reputation: 3439

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.

Upvotes: 6

Punit Raizada
Punit Raizada

Reputation: 494

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.

Upvotes: 15

Related Questions