Twitter + OAuth 集成 [英] Twitter + OAuth Integration
问题描述
任何人都可以使用 OAuth 在 Android + Twitter 集成方面提供帮助.
Anybody can please help in Android + Twitter Integration using OAuth.
我已经在http://github.com/brione/Brion-Learns-OAuth 并收到下面列出的错误,当我发布状态更新时...
I already worked on http://github.com/brione/Brion-Learns-OAuth and getting the error listed below, when I am posting status update...
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 并存储在首选项中...
I succeed with OAuth Authentication and getting user_secret and user_token and stored in preferences...
所以问题在于使用 OAuth 标头进行 http 发布...
So the issue is with http posting using OAuth header...
而我的 Http Post 方法如下:
and My Http Post Method is as :
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));
}
}
}
推荐答案
请描述您使用的是什么 Client/Consumer/Provider,它们必须是 DefaultHttpClient
/CommonsHttpOAuthConsumer
/CommonsHttpOAuthProvider
肯定能正常工作.
Please describe what Client/Consumer/Provider you are using, they must be DefaultHttpClient
/CommonsHttpOAuthConsumer
/CommonsHttpOAuthProvider
to work properly for sure.
确保在调用此代码之前调用 consumer.setTokenWithSecret(oToken, oTokenSecret);
.
Ensure you call consumer.setTokenWithSecret(oToken, oTokenSecret);
before calling this code.
此外,post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
是否存在于您的帖子参数中?
Also, is post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
exists in your post params?
使用空的 BasicResponseHandler
的原因是什么,我想它什么都不处理,并且可以在 execute
调用中省略.
What's the reason for using empty BasicResponseHandler
, it handles nothing and it can be omitted in execute
call, I suppose.
而且,可能是一个愚蠢的问题,可能是您在 setEntity(...)
And, may be a dumb question, may be you are overwriting params when calling setParams(...)
after setEntity(...)
这篇关于Twitter + OAuth 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!