有没有人为Android推特提供OAuth认证的工作示例?我曾试图同时使用Twitter4J和SignPost,但我得到了非常奇怪的错误,说twitter.com是一个未知的主机。我已经知道SignPost库和Android存在问题(under Google 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();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
}
这是我的Twitter4J代码:
private void getReqTokenAndAuthenticateUsingTwitter4J() {
String callbackUrl = "twitter-test:///";
try {
// Prepare a request token, based on consumer key and consumer
// secret
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(CONSUMER_KEY);
builder.setOAuthConsumerSecret(CONSUMER_SECRET);
Configuration cfg = builder.build();
Twitter t = new TwitterFactory(cfg).getInstance();
RequestToken requestToken = t.getOAuthRequestToken(callbackUrl);
String tokenStr = requestToken.getToken();
String tokenSecretStr = requestToken.getTokenSecret();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = preferences.edit();
edit.putString(REQ_TOKEN, tokenStr);
edit.putString(REQ_TOKEN_SECRET, tokenSecretStr);
if (edit.commit()) {
// Launch the browser for authentication in twitter's web-gui
Uri authenticationUri = Uri.parse(requestToken.getAuthenticationURL());
startActivity(new Intent(Intent.ACTION_VIEW, authenticationUri));
}
} catch (TwitterException e) {
e.printStackTrace();
}
}
是的,我有一个带有Internet权限的清单和一个用于callback-url的intent过滤器。以下是运行SignPost代码时logcat的输出:
W/System.err( 779): oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: twitter.com
W/System.err( 779): at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:214)
W/System.err( 779): at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:69)
W/System.err( 779): at com.examples.twitterimages.TwitterImagesActivity.getReqTokenAndAuthenticateUsingSignPost(TwitterImagesActivity.java:109)
W/System.err( 779): at com.examples.twitterimages.TwitterImagesActivity.onCreate(TwitterImagesActivity.java:64)
W/System.err( 779): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err( 779): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
W/System.err( 779): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
W/System.err( 779): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
W/System.err( 779): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
W/System.err( 779): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err( 779): at android.os.Looper.loop(Looper.java:123)
W/System.err( 779): at android.app.ActivityThread.main(ActivityThread.java:3701)
W/System.err( 779): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 779): at java.lang.reflect.Method.invoke(Method.java:507)
W/System.err( 779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
W/System.err( 779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
W/System.err( 779): at dalvik.system.NativeStart.main(Native Method)
W/System.err( 779): Caused by: java.net.UnknownHostException: twitter.com
W/System.err( 779): at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
W/System.err( 779): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
W/System.err( 779): at java.net.InetAddress.getAllByName(InetAddress.java:256)
W/System.err( 779): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
W/System.err( 779): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
W/System.err( 779): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
W/System.err( 779): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
W/System.err( 779): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err( 779): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err( 779): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err( 779): at oauth.signpost.commonshttp.CommonsHttpOAuthProvider.sendRequest(CommonsHttpOAuthProvider.java:64)
W/System.err( 779): at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:177)
W/System.err( 779): ... 16 more
这是运行Twitter4J代码时的输出:
W/System.err( 817): api.twitter.comRelevant discussions can be on the Internet at:
W/System.err( 817): http://www.google.co.jp/search?q=6c607809 or
W/System.err( 817): http://www.google.co.jp/search?q=0f1d8134
W/System.err( 817): TwitterException{exceptionCode=[6c607809-0f1d8134 6c607809-0f1d810a], statusCode=-1, retryAfter=0, rateLimitStatus=null, version=2.2.1}
W/System.err( 817): at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:204)
W/System.err( 817): at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
W/System.err( 817): at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102)
W/System.err( 817): at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:108)
W/System.err( 817): at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:271)
W/System.err( 817): at com.examples.twitterimages.TwitterImagesActivity.getReqTokenAndAuthenticateUsingTwitter4J(TwitterImagesActivity.java:136)
W/System.err( 817): at com.examples.twitterimages.TwitterImagesActivity.onCreate(TwitterImagesActivity.java:64)
W/System.err( 817): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err( 817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
W/System.err( 817): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
W/System.err( 817): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
W/System.err( 817): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
W/System.err( 817): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err( 817): at android.os.Looper.loop(Looper.java:123)
W/System.err( 817): at android.app.ActivityThread.main(ActivityThread.java:3701)
W/System.err( 817): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 817): at java.lang.reflect.Method.invoke(Method.java:507)
W/System.err( 817): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
W/System.err( 817): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
W/System.err( 817): at dalvik.system.NativeStart.main(Native Method)
W/System.err( 817): Caused by: java.net.UnknownHostException: api.twitter.com
W/System.err( 817): at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
W/System.err( 817): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
W/System.err( 817): at java.net.InetAddress.getAllByName(InetAddress.java:256)
W/System.err( 817): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
W/System.err( 817): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
W/System.err( 817): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
W/System.err( 817): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
W/System.err( 817): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
W/System.err( 817): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
W/System.err( 817): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
W/System.err( 817): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614)
W/System.err( 817): at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:162)
W/System.err( 817): ... 19 more
有人解决了吗?我被困在这里我可以将代码作为Java SE程序运行,但它在android上失败...
// Jonas
答案 0 :(得分:12)
检查两个步骤
1- 在Twitter网站上为您创建的Twitter应用中提供回拨网址(转到您的app-&gt; settings-&gt;回调网址) Click here
2- 转到手机设置,然后点击“日期和时间”并选择“自动”(请确保您的设备时区和时间,日期都正确)如果你的时间,日期和时区不正确,你不能发推文。
答案 1 :(得分:3)
我有一个为Twitter with Twitter4J on Android here设置OAuth的教程,它包含了要下载的完整项目源代码 - 这样可能会有所帮助。
至于您的具体问题 - 您是否可以通过Android模拟器连接到互联网?它看起来好像无法连接 - 您是否在模拟器上有任何其他尝试连接互联网的应用程序,您可以尝试进行测试?有时当我运行模拟器时,如果互联网连接失败,那么模拟器上的信号栏会出现一个X和它的无条件。
答案 2 :(得分:2)
我在Android中使用SignPost完全适用于Twitter客户端,工作正常。
答案 3 :(得分:1)
是的,我解决了(我上面写的)。我弄乱了我的清单并拼写了互联网许可证。 Ť
答案 4 :(得分:0)
对我来说,我使用http:而不是https:作为请求URI的