得到错误:"无法验证oauth签名和令牌"从Android集成twitter

时间:2015-02-18 08:45:12

标签: android android-twitter

我正在尝试使用twitter4j-core登录twitter ...我的示例代码位于

之下
@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();
    }
  }
 });

}

如果用户未登录且点击后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;
}

} 

它显示了一个webview登录活动并回调当前活动。所以在onResume中我保存了accessstoken和其他东西来保存session.like:

@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");
      }
  } 

这段代码很好用了几次。但现在它没有用。显示以下错误:

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)

我做错了什么?我遵循本教程http://blog.blundell-apps.com/sending-a-tweet/

1 个答案:

答案 0 :(得分:0)

请检查您的手机时间是否设置正确。如果未同步服务器时间和机器时间,则会发生此错误。