2010-09-15 115 views
1

任何人都可以在使用OAuth的Android + Twitter集成中請求幫助。Twitter + OAuth集成

我已經工作的http://github.com/brione/Brion-Learns-OAuth和得到以下列出的錯誤,當我張貼的狀態更新...

WARN/System.err(190): org.apache.http.client.HttpResponseException: Unauthorized 
WARN/System.err(190):  at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71) 
WARN/System.err(190):  at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59) 
WARN/System.err(190):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657) 
WARN/System.err(190):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627) 
WARN/System.err(190):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616) 
WARN/System.err(190):  at com.test.twitter.BLOA$PostTask.doInBackground(BLOA.java:343) 
WARN/System.err(190):  at com.test.twitter.BLOA$PostTask.doInBackground(BLOA.java:1) 
WARN/System.err(190):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
WARN/System.err(190):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256) 
WARN/System.err(190):  at java.util.concurrent.FutureTask.run(FutureTask.java:122) 
WARN/System.err(190):  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648) 
WARN/System.err(190):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673) 
WARN/System.err(190):  at java.lang.Thread.run(Thread.java:1060) 

我與OAuth認證成功,並獲得user_secret和user_token並存儲在首選項...

所以這個問題是使用OAuth HTTP頭髮布...

和我的HTTP POST方法是:

private class PostTask extends AsyncTask<String, Void, JSONObject> { 

    ProgressDialog postDialog; 

    @Override 
    protected void onPreExecute() { 
    postDialog = ProgressDialog.show(BLOA.this, 
    getText(R.string.tweet_progress_title), 
    getText(R.string.tweet_progress_text), true, // indeterminate 
       // duration 
    false); // not cancel-able 
    } 

    @Override 
    protected JSONObject doInBackground(String... params) { 

    JSONObject jso = null; 
    try { 
    HttpPost post = new HttpPost(
     "http://twitter.com/statuses/update.json"); 
    LinkedList<BasicNameValuePair> out = new LinkedList<BasicNameValuePair>(); 
    out.add(new BasicNameValuePair("status", params[0])); 
    post.setEntity(new UrlEncodedFormEntity(out, HTTP.UTF_8)); 
    post.setParams(getParams()); 
    // sign the request to authenticate 
    mConsumer.sign(post); 
    String response = mClient.execute(post, 
     new BasicResponseHandler()); 
    jso = new JSONObject(response); 
    } catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
    } catch (OAuthMessageSignerException e) { 
    e.printStackTrace(); 
    } catch (OAuthExpectationFailedException e) { 
    e.printStackTrace(); 
    } catch (OAuthCommunicationException e) { 
    e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } catch (JSONException e) { 
    e.printStackTrace(); 
    } finally { 

    } 
    return jso; 
    } 

    // This is in the UI thread, so we can mess with the UI 
    protected void onPostExecute(JSONObject jso) { 
    postDialog.dismiss(); 
    if (jso != null) { // authorization succeeded, the json object 
    // contains the user information 
    mEditor.setText(""); 
    mLast.setText(getCurrentTweet(jso)); 
    } else { 
    mLast.setText(getText(R.string.tweet_error)); 
    } 
    } 
} 
+0

新OAuth教程調用setParams(...)時是要覆蓋PARAMS:http://goo.gl/99vpL – Blundell 2011-07-24 18:04:55

回答

1

請描述你所使用的客戶端/消費/供應商,他們必須DefaultHttpClient/CommonsHttpOAuthConsumer/CommonsHttpOAuthProvider爲確保正常工作。

確保您在致電此代碼前致電consumer.setTokenWithSecret(oToken, oTokenSecret);

另外,post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);是否存在於你的post-params?

什麼原因使用空BasicResponseHandler,它不處理任何東西,它可以省略execute調用,我想。

而且,可能是一個愚蠢的問題,可能以後setEntity(...)

2

雖然您在onResume()中成功接收到user_secret和user_token,但您確定您的原始對象仍然相同嗎?我在Android應用中遇到了這個問題。我會創建這些對象,但是當調用onResume()時,它是一個完全新的Activity實例,因爲它在瀏覽器啓動時從內存中釋放。所以當我試圖設置返回的祕密/令牌對時,它將不起作用。這種情況更可能發生在內存有限的設備上。有些人選擇在呼叫之間堅持必要的信息,而其他人則決定不啓動默認瀏覽器意圖,而是主持嵌入式Web視圖,以便其原始路標-oauth對象不會超出範圍。

OAuth instance state in Android

不知道這是否是問題,但也許值得一試。

1

我有2個教程2個不同的Java庫。 First one(日期)在這裏,2nd one here與Scribe。它適用於LinkedIn,但切換到Twitter會非常容易。我會去#2

2

您需要使用post.addHeader()將oauth信息添加到http請求的標頭中。要知道添加到頁眉哪些事情,看看這裏:http://dev.twitter.com/pages/auth

+0

我的代碼上面寫的..你可以請詳細解釋一下?或者給我一個例子? – 2010-09-25 12:12:09

+0

您正在使用路標庫來簡化使用OAuth協議的交互,因此不需要手動設置標題。請回答我的問題,我會盡力幫助你,我也在我的項目中使用路標庫。 – 2010-09-25 16:08:00