Twitter issue in android:: com.twitter.sdk.android.core.TwitterApiException: HTTP request failed, Status: 400

Viewed 1852

can you please resolve the issue having while integrating the twitter login in my android app.

here is the issues :

Twitter: Failed to get request token com.twitter.sdk.android.core.TwitterApiException: HTTP request failed, Status: 400 at com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42) at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5643) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Twitter: Authorization completed with an error com.twitter.sdk.android.core.TwitterAuthException: Failed to get request token at com.twitter.sdk.android.core.identity.OAuthController$1.failure(OAuthController.java:94) at com.twitter.sdk.android.core.internal.oauth.OAuth1aService$1.failure(OAuth1aService.java:191) at com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42) at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5643) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

And here is my code :

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TwitterConfig config = new TwitterConfig.Builder(this)
            .logger(new DefaultLogger(Log.DEBUG))
            .twitterAuthConfig(new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET))
            .debug(true)
            .build();
    Twitter.initialize(config);
    setContentView(R.layout.layout_list);

    nameit = (TextView) findViewById(R.id.nameit);
    loginButton = (TwitterLoginButton) findViewById(R.id.login_button);

    loginButton.setCallback(new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) {
            // Do something with result, which provides a TwitterSession for making API calls
            try {
                TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
                TwitterAuthToken authToken = session.getAuthToken();
                String token = authToken.token;
                String secret = authToken.secret;
                TwitterSession session1 = result.data;
                String name = session1.getUserName();
                nameit.setText("Welcome " + name);
                Log.e("DATTTA", session.getUserId() + "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void failure(TwitterException exception) {
            // Do something on failure
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    loginButton.onActivityResult(requestCode, resultCode, data);
}

Sorry for bad English and Thanks in advance.

2 Answers

I had same problem and I solved it by the following methods Those are

1) checked for callback URL and added extra CallBack URL i.e

twittersdk://

adding the above url because if we don’t have twitter app installed the twittersdk tries to open in web view so we have to add this callback url

2)Added this code before SetContentView method in onCreate

//Initialize twitter SDK
TwitterConfig config = new TwitterConfig.Builder(this)
                .logger(new DefaultLogger(Log.DEBUG))
                .twitterAuthConfig(new TwitterAuthConfig(YOUR_CONSUMER_API_KEY, YOUR_CONSUMER_API_SECRET))
                .debug(true)
                .build();
Twitter.initialize(config);
Related