Android twitter oauth错误 - 过期令牌请求

时间:2010-11-02 21:42:03

标签: android twitter oauth

private static String requestToken;
 private static String requestSecret;

 public static void loginTwitter(Context mycontext) {

  if (savedToken==null) {
   try {
    consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
    provider = new CommonsHttpOAuthProvider("https://api.twitter.com/oauth/request_token",
              "https://api.twitter.com/oauth/access_token",
              "https://api.twitter.com/oauth/authorize");

    String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
    Toast.makeText(mycontext, "login to twitter", Toast.LENGTH_LONG).show();

    mycontext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));

   } catch (Exception e) {
    Log.e(APP, e.getMessage());
   }
  } else {

   tweetShareURL();
  }
 }

 private static void tweetShareURL() {

  twitter = new TwitterFactory().getInstance();
  twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
  twitter.setOAuthAccessToken(savedToken);

  // create a tweet
  String tweet = title+" " + url;

  try {
   twitter.updateStatus(tweet);
  } catch (TwitterException e) {
   Log.e(APP, "2 "+e.getMessage());

  } finally {
   utils.hint("tweet sent!");
  }


 }

 static AccessToken savedToken=null;

 @Override
 protected void onNewIntent(Intent intent) {
  super.onNewIntent(intent);

  Uri uri = intent.getData();
  Log.e("URI", ""+uri.toString());

  if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {

   String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
   Log.e("verifier", ""+verifier);

   try {
    requestToken  = consumer.getToken();
    requestSecret = consumer.getConsumerSecret();

    consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
    consumer.setTokenWithSecret(requestToken, requestSecret);

    provider = new CommonsHttpOAuthProvider("https://api.twitter.com/oauth/request_token",
              "https://api.twitter.com/oauth/access_token",
              "https://api.twitter.com/oauth/authorize");
    provider.setOAuth10a(true);

    // TODO: you might want to store token and token_secret in you app settings!!!!!!!!
    savedToken = new AccessToken(requestToken, requestSecret);

    provider.retrieveAccessToken(consumer, verifier);

    Log.e(APP, "2");

    tweetShareURL();

   } catch (Exception e) {
    Log.e(APP, "1 "+e.getMessage());
   }

  }
 }
总是得到这样的话:

11-02 22:36:39.526:ERROR / OAUTH(424):2 401:身份验证凭据丢失或不正确。 11-02 22:36:39.526:ERROR / OAUTH(424):{“error”:“无效\ /过期令牌”,“请求”:“\ / 1 \ / statuses \ /update.json”}

为什么呢?

1 个答案:

答案 0 :(得分:0)

"\/1\/statuses\/update.json"当你在引号中有东西时,它会在引号中以字符串形式发送所有内容。那么转义字符\或许正在阻碍它?