2011-05-08 55 views
1

連接我有我在哪裏得到一個401錯誤的例子,因爲消費者不正確或簽名不匹配。如何把這裏簽名密鑰,因爲在我的工作Java示例有一個簽名密鑰我想實現它。我的應用程序與Twitter

public class TwitterApp { 
private Twitter mTwitter; 
private TwitterSession mSession; 
private AccessToken mAccessToken; 
private OAuthConsumer mHttpOauthConsumer; 
private OAuthProvider mHttpOauthprovider; 
private String mConsumerKey; 
private String mSecretKey; 
private ProgressDialog mProgressDlg; 
private TwDialogListener mListener; 
private Context context; 

public static final String CALLBACK_URL = "twitterapp://connect"; 
private static final String TAG = "TwitterApp"; 

public TwitterApp(Context context, String consumerKey, String secretKey) { 
    this.context = context; 

    mTwitter  = new TwitterFactory().getInstance(); 
    mSession  = new TwitterSession(context); 
    mProgressDlg = new ProgressDialog(context); 

    mProgressDlg.requestWindowFeature(Window.FEATURE_NO_TITLE); 

    mConsumerKey = consumerKey; 
    mSecretKey  = secretKey; 

    mHttpOauthConsumer = new CommonsHttpOAuthConsumer(mConsumerKey, mSecretKey); 
    mHttpOauthprovider = new CommonsHttpOAuthProvider("http://twitter.com/oauth/request_token", 
               "http://twitter.com/oauth/access_token", 
               "http://twitter.com/oauth/authorize"); 

    mAccessToken = mSession.getAccessToken(); 

    configureToken(); 
} 

public void setListener(TwDialogListener listener) { 
    mListener = listener; 
} 

@SuppressWarnings("deprecation") 
private void configureToken() { 
    if (mAccessToken != null) { 
     mTwitter.setOAuthConsumer(mConsumerKey, mSecretKey); 

     mTwitter.setOAuthAccessToken(mAccessToken); 
    } 
} 

public boolean hasAccessToken() { 
    return (mAccessToken == null) ? false : true; 
} 

public void resetAccessToken() { 
    if (mAccessToken != null) { 
     mSession.resetAccessToken(); 

     mAccessToken = null; 
    } 
} 

public String getUsername() { 
    return mSession.getUsername(); 
} 

public void updateStatus(String status) throws Exception { 
    try { 
     mTwitter.updateStatus(status); 
    } catch (TwitterException e) { 
     throw e; 
    } 
} 

public void authorize() { 
    mProgressDlg.setMessage("Initializing ..."); 
    mProgressDlg.show(); 

    new Thread() { 
     @Override 
     public void run() { 
      String authUrl = ""; 
      int what = 1; 

      try { 
       authUrl = mHttpOauthprovider.retrieveRequestToken(mHttpOauthConsumer, CALLBACK_URL);  

       what = 0; 

       Log.d(TAG, "Request token url " + authUrl); 
      } catch (Exception e) { 
       Log.d(TAG, "Failed to get request token"); 

       e.printStackTrace(); 
      } 

      mHandler.sendMessage(mHandler.obtainMessage(what, 1, 0, authUrl)); 
     } 
    }.start(); 
} 

public void processToken(String callbackUrl) { 
    mProgressDlg.setMessage("Finalizing ..."); 
    mProgressDlg.show(); 

    final String verifier = getVerifier(callbackUrl); 

    new Thread() { 
     @Override 
     public void run() { 
      int what = 1; 

      try { 
       mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer, verifier); 

       mAccessToken = new AccessToken(mHttpOauthConsumer.getToken(), mHttpOauthConsumer.getTokenSecret()); 

       configureToken(); 

       User user = mTwitter.verifyCredentials(); 

       mSession.storeAccessToken(mAccessToken, user.getName()); 

       what = 0; 
      } catch (Exception e){ 
       Log.d(TAG, "Error getting access token"); 

       e.printStackTrace(); 
      } 

      mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0)); 
     } 
    }.start(); 
} 

private String getVerifier(String callbackUrl) { 
    String verifier = ""; 

    try { 
     callbackUrl = callbackUrl.replace("twitterapp", "http"); 

     URL url   = new URL(callbackUrl); 
     String query = url.getQuery(); 

     String array[] = query.split("&"); 

     for (String parameter : array) { 
      String v[] = parameter.split("="); 

      if (URLDecoder.decode(v[0]).equals(oauth.signpost.OAuth.OAUTH_VERIFIER)) { 
       verifier = URLDecoder.decode(v[1]); 
       break; 
      } 
     } 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 

    return verifier; 
} 

private void showLoginDialog(String url) { 
    final TwDialogListener listener = new TwDialogListener() { 
     @Override 
     public void onComplete(String value) { 
      processToken(value); 
     } 

     @Override 
     public void onError(String value) { 
      mListener.onError("Failed opening authorization page"); 
     } 
    }; 

    new TwitterDialog(context, url, listener).show(); 
} 

private Handler mHandler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     mProgressDlg.dismiss(); 

     if (msg.what == 1) { 
      if (msg.arg1 == 1) 
       mListener.onError("Error getting request token"); 
      else 
       mListener.onError("Error getting access token"); 
     } else { 
      if (msg.arg1 == 1) 
       showLoginDialog((String) msg.obj); 
      else 
       mListener.onComplete(""); 
     } 
    } 
}; 

public interface TwDialogListener { 
    public void onComplete(String value);  

    public void onError(String value); 
} 

}

我有Java也的工作例子,我很容易得到令牌但事情是它採用DefaultOAuthConsumer和DefaultOAuthProvider但每當我試圖實現它在我的例子中,我得到通信錯誤,這就是爲什麼我使用CommonsHttpOAuthConsumer和CommonsHttpOAuthProvider。

這裏是java的工作代碼

公共類主要{

public static void main(String[] args) throws Exception { 

    OAuthConsumer consumer = new DefaultOAuthConsumer(
      "iIlNngv1KdV6XzNYkoLA", 
      "exQ94pBpLXFcyttvLoxU2nrktThrlsj580zjYzmoM", 
      SignatureMethod.HMAC_SHA1); 

    OAuthProvider provider = new DefaultOAuthProvider(consumer, 
      "http://twitter.com/oauth/request_token", 
      "http://twitter.com/oauth/access_token", 
      "http://twitter.com/oauth/authorize"); 

    System.out.println("Fetching request token from Twitter..."); 

    // we do not support callbacks, thus pass OOB 
    String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND); 

    System.out.println("Request token: " + consumer.getToken()); 
    System.out.println("Token secret: " + consumer.getTokenSecret()); 

    System.out.println("Now visit:\n" + authUrl 
      + "\n... and grant this app authorization"); 
    System.out.println("Enter the PIN code and hit ENTER when you're done:"); 

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    String pin = br.readLine(); 

    System.out.println("Fetching access token from Twitter..."); 

    provider.retrieveAccessToken(pin); 

    System.out.println("Access token: " + consumer.getToken()); 
    System.out.println("Token secret: " + consumer.getTokenSecret()); 

    URL url = new URL("http://twitter.com/statuses/mentions.xml"); 
    HttpURLConnection request = (HttpURLConnection) url.openConnection(); 

    consumer.sign(request); 

    System.out.println("Sending request to Twitter..."); 
    request.connect(); 

    System.out.println("Response: " + request.getResponseCode() + " " 
      + request.getResponseMessage()); 
} 

}

,我還沒有意識到的回調URL字符串我在這裏使用它..

+0

我想在這裏建議是這樣,請不要在Android上使用DefaultOAuth *實現,相反,使用CommonsHttpOAuth *類,因爲他們是爲了與Apache下議院HTTP使用(這就是Android使用了HTTP反正) 。這是Android中的一個bug,Android的java.net.HttpURLConnection使它無法與某些服務提供者合作。謝謝 – PiyushMishra 2011-05-08 10:59:43

回答

0

的CALLBACK_URL應匹配

<intent-filter> 

在你的manifest.xml中。

在你的代碼片段,讓你聲明並初始化mHttpOathCosumer和供應商?

+0

不,我沒有,我怎麼能,你可以給我一些例子。我做了一些變化,它也像現在我看到了谷歌的網站,他們說,不要使用DefaultOAuth *實現在Android相反,使用CommonsHttpOAuth *它在Android的java.net.HttpURLConnection中的一個bug所以現在我使用這個,並得到了答覆,但由於消費者密鑰不正確或簽名不匹配,但仍然獲得授權失敗,但我的消費者密鑰是正確的。幫助我 – PiyushMishra 2011-05-08 10:57:38

+0

是的。根據Android,你不應該使用DefaultOauth。這兩個主題可能對你有所幫助。 http://stackoverflow.com/questions/1965568/oauth-instance-state-in-android和http://stackoverflow.com/questions/1357372/ioexception-received-authentication-challenge-is-null-apache-harmony- android – nahwarang 2011-05-08 11:22:55

+0

是的,我已經看到了這些鏈接,但仍然沒有得到我的答案。事實與他們非常相似是的,我得到了同樣的錯誤401,但仍然不知道如何解決它。 :( – PiyushMishra 2011-05-08 11:49:02

相關問題