有没有人对Android的Twitter进行OAuth身份验证的工作示例?我曾试图同时使用Twitter4J和SignPost,但我得到了非常奇怪的错误,说twitter.com是一个未知的主机.我已经知道使用SignPost库和Android 存在问题(在谷歌Android下),并且根据项目的主页,CommonsHttpOAuth*类应该有效.
这是我的SignPost:
private void getReqTokenAndAuthenticateUsingSignPost() {
String callbackUrl = "twitter-test:///";
CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
CONSUMER_SECRET);
CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider(
"http://twitter.com/oauth/request_token", "http://twitter.com/oauth/access_token",
"http://twitter.com/oauth/authorize");
String tokenStr = consumer.getToken();
String tokenSecretStr = consumer.getTokenSecret();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = preferences.edit();
edit.putString(REQ_TOKEN, tokenStr);
edit.putString(REQ_TOKEN_SECRET, tokenSecretStr);
try {
String authUrl = provider.retrieveRequestToken(consumer, callbackUrl);
Uri authenticationUri = Uri.parse(authUrl);
startActivity(new Intent(Intent.ACTION_VIEW, authenticationUri));
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} …Run Code Online (Sandbox Code Playgroud)