Failed to connect to Twitter API using TwitterKit on Android


#1

I’m using twitter-kit to make authentication with twitter. I choose to use TwitterAuthClient instead of TwitterLoginButton. Everything goes fine when I tested on emulator (genymotion), but when I tried with real device (Nexus 5, android 4.4), this error occurs

com.twitter.sdk.android.core.TwitterAuthException: Failed to get request token
        at com.twitter.sdk.android.core.identity.OAuthController$1.failure(OAuthController.java:74)
        at com.twitter.sdk.android.core.internal.oauth.OAuth1aService$1.failure(OAuth1aService.java:215)
        at com.twitter.sdk.android.core.Callback.failure(Callback.java:28)
        at retrofit.CallbackRunnable$2.run(CallbackRunnable.java:53)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
 Caused by: com.twitter.sdk.android.core.TwitterApiException: failed to connect to api.twitter.com/5.22.148.51 (port 443) after 15000ms: isConnected failed: EHOSTUNREACH (No route to host)
        at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:389)
        at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
        at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
        at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at retrofit.Platform$Android$2$1.run(Platform.java:142)
        at java.lang.Thread.run(Thread.java:811)

It said fail to connect after 15000ms but I get that error right away when trying to go to authen flow.

Do anyone have a way to fix it ?


#2

Hi there,

From the stack trace, it looks like a network connectivity issue,

Caused by: com.twitter.sdk.android.core.TwitterApiException: failed to connect to api.twitter.com/5.22.148.51 (port 443) after 15000ms: isConnected failed: EHOSTUNREACH (No route to host)

Are you able to access twitter from your device’s web browser?


#3

If dont install Twitter client, error log.

E/Twitter (32531): com.twitter.sdk.android.core.TwitterApiException: 401 Authorization Required
E/Twitter (32531): 	at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:388)
E/Twitter (32531): 	at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
E/Twitter (32531): 	at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
E/Twitter (32531): 	at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
E/Twitter (32531): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/Twitter (32531): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/Twitter (32531): 	at retrofit.Platform$Android$2$1.run(Platform.java:142)
E/Twitter (32531): 	at java.lang.Thread.run(Thread.java:818)
E/Twitter (32531): Authorization completed with an error
E/Twitter (32531): com.twitter.sdk.android.core.TwitterAuthException: Failed to get request token
E/Twitter (32531): 	at com.twitter.sdk.android.core.identity.OAuthController$1.failure(OAuthController.java:78)
E/Twitter (32531): 	at com.twitter.sdk.android.core.internal.oauth.OAuth1aService$1.failure(OAuth1aService.java:215)
E/Twitter (32531): 	at com.twitter.sdk.android.core.Callback.failure(Callback.java:28)
E/Twitter (32531): 	at retrofit.CallbackRunnable$2.run(CallbackRunnable.java:53)
E/Twitter (32531): 	at android.os.Handler.handleCallback(Handler.java:739)
E/Twitter (32531): 	at android.os.Handler.dispatchMessage(Handler.java:95)
E/Twitter (32531): 	at android.os.Looper.loop(Looper.java:135)
E/Twitter (32531): 	at android.app.ActivityThread.main(ActivityThread.java:5221)
E/Twitter (32531): 	at java.lang.reflect.Method.invoke(Native Method)
E/Twitter (32531): 	at java.lang.reflect.Method.invoke(Method.java:372)
E/Twitter (32531): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
E/Twitter (32531): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
W/System.err(32531): com.twitter.sdk.android.core.TwitterAuthException: Failed to get request token
W/System.err(32531): 	at com.twitter.sdk.android.core.identity.OAuthController$1.failure(OAuthController.java:78)
W/System.err(32531): 	at com.twitter.sdk.android.core.internal.oauth.OAuth1aService$1.failure(OAuth1aService.java:215)
W/System.err(32531): 	at com.twitter.sdk.android.core.Callback.failure(Callback.java:28)
W/System.err(32531): 	at retrofit.CallbackRunnable$2.run(CallbackRunnable.java:53)
W/System.err(32531): 	at android.os.Handler.handleCallback(Handler.java:739)
W/System.err(32531): 	at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err(32531): 	at android.os.Looper.loop(Looper.java:135)
W/System.err(32531): 	at android.app.ActivityThread.main(ActivityThread.java:5221)
W/System.err(32531): 	at java.lang.reflect.Method.invoke(Native Method)
W/System.err(32531): 	at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err(32531): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
W/System.err(32531): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
W/FlurryAgent(32531): Flurry session started for context:com.oxa7.shou.LandingActivity@9a83c34

#4

Hello,

If you don’t have the Twitter client installed, we fall back to webview to complete the authorization flow. Some things to check:

  1. Your device time is correct and up to date
  2. “Allow this application to be used to Sign in with Twitter” is set in your twitter app’s settings.
  3. You have a callback url configured for your twitter app.

Lien


#5

Thank you, add callback url work well, please add top on website when developer create app.