Search code examples
androidandroid-twitter

getting error: "Failed to validate oauth signature and token" in integrating twitter from Android


I am trying to login twitter using twitter4j-core... my sample code is below

@Override
 protected void onCreate(Bundle savedInstanceState) {
  .......
  .....
  ...
  ..
  // Tell twitter4j that we want to use it with our app
  ConfigurationBuilder builder = new ConfigurationBuilder();
  builder.setDebugEnabled(true);
  builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
  builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
  Configuration configuration = builder.build();
  TwitterFactory factory = new TwitterFactory(configuration);
  twitter = factory.getInstance();


  /**
   * Twitter login button click event will call loginToTwitter() function
   * */
  btnTwitterLogoutLogin.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

    if (isTwitterLoggedInAlready()) {
    logoutFromTwitter();
             } else {
    new LoginNewUser().execute();
    }
  }
 });

}

if user not logged-in and after clicked btnTwitterLogoutLogin=>

class LoginNewUser extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub

    try {

    requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
    PostTwitter.this.startActivity(new Intent(Intent.ACTION_VIEW,
                                               Uri.parse(requestToken.getAuthenticationURL())));
    } catch (TwitterException e) {
    e.printStackTrace();
    }
    return null;
}

} 

it show's a webview login activity and call back to the current activity.So in onResume i save the accesstoken and other things to save session.like this:

@Override
protected void onResume() {
   super.onResume();
   Log.i("TAG", "Arrived at onResume");
   dealWithTwitterResponse();
}

private void dealWithTwitterResponse() {
  Uri uri = getIntent().getData();
    if (uri !=null&&uri.toString().startsWith(TWITTER_CALLBACK_URL))                    
      {
          // If the// user// has// just// logged// in
          String oauthVerifier = uri.getQueryParameter("oauth_verifier");
          Log.d("DEBUG", "loggged in");
          new SaveTokenAfterLogin().execute(oauthVerifier);
          // in this method i save accesstoken for further use in sharepreferance 
      }
  else
      {
        Log.d("DEBUG", "Not logged in");
      }
  } 

this code work's fine for couple of time. but now it's not working. showing bellow error:

02-18 07:55:56.885: W/System.err(6659): 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.
02-18 07:55:56.885: W/System.err(6659): Failed to validate oauth signature and token
02-18 07:55:56.889: W/System.err(6659): Relevant discussions can be found on the Internet at:
02-18 07:55:56.889: W/System.err(6659):     http://www.google.co.jp/search?q=8e063946 or
02-18 07:55:56.889: W/System.err(6659):     http://www.google.co.jp/search?q=c60cdc4a
02-18 07:55:56.889: W/System.err(6659): TwitterException{exceptionCode=[8e063946-c60cdc4a], statusCode=401, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=4.0.2}
02-18 07:55:56.893: W/System.err(6659):     at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:163)
02-18 07:55:56.893: W/System.err(6659):     at twitter4j.HttpClientBase.request(HttpClientBase.java:53)

what's I am doing wrong? I following this tutorial http://blog.blundell-apps.com/sending-a-tweet/


Solution

  • Please check if your phone time is set correctly. This error can occur when the server time and machine time are not synced.