android :: com.twitter.sdk.android.core.TwitterApiException中的Twitter问题:HTTP请求失败,状态:400

时间:2017-08-09 08:14:18

标签: android

你可以解决在我的Android应用程序中集成twitter登录时遇到的问题。

这是问题:

  

Twitter:无法获得请求令牌                                                               com.twitter.sdk.android.core.TwitterApiException:HTTP请求失败,状态:400                                                                   在com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42)                                                                   at retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ 1.run(ExecutorCallAdapterFactory.java:68)                                                                   在android.os.Handler.handleCallback(Handler.java:815)                                                                   在android.os.Handler.dispatchMessage(Handler.java:104)                                                                   在android.os.Looper.loop(Looper.java:194)                                                                   在android.app.ActivityThread.main(ActivityThread.java:5643)                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                   在java.lang.reflect.Method.invoke(Method.java:372)                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:960)                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

     

Twitter:授权已完成,但出现错误                                                               com.twitter.sdk.android.core.TwitterAuthException:无法获取请求令牌                                                                   在com.twitter.sdk.android.core.identity.OAuthController $ 1.failure(OAuthController.java:94)                                                                   在com.twitter.sdk.android.core.internal.oauth.OAuth1aService $ 1.failure(OAuth1aService.java:191)                                                                   在com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42)                                                                   at retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ 1.run(ExecutorCallAdapterFactory.java:68)                                                                   在android.os.Handler.handleCallback(Handler.java:815)                                                                   在android.os.Handler.dispatchMessage(Handler.java:104)                                                                   在android.os.Looper.loop(Looper.java:194)                                                                   在android.app.ActivityThread.main(ActivityThread.java:5643)                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                   在java.lang.reflect.Method.invoke(Method.java:372)                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:960)                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

这是我的代码:

    @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);
}

抱歉英语不好,并提前致谢。

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,我按照以下步骤解决了这个问题。

转到Twitter应用程序管理 点击您当前正在使用的“应用” 点击“设置”标签

1)回调网址:插入任何有效的网址,例如 http://www.google.com 当您的设备中未安装Twitter应用程序时,回调URL用于在浏览器上重定向。

2)启用回叫锁定(建议启用回调锁定以确保应用无法覆盖回调URL):验证是否取消选中

3)无需在应用程序类中初始化您的twitter sdk,而是将其初始化如下:

    private void init(){

    TwitterAuthConfig authConfig = new TwitterAuthConfig(getResources().getString(R.string.consumer_key), getResources().getString(R.string.secrete_key));
    TwitterConfig config = new TwitterConfig.Builder(this)
            .logger(new DefaultLogger(Log.DEBUG))
            .twitterAuthConfig(authConfig)
            .debug(true)
            .build();
    Twitter.initialize(config);

}

谢谢..

答案 1 :(得分:0)

我遇到了同样的问题,并通过以下方法解决了 那些是

1)检查了回调URL,并添加了额外的回调URL,即

twittersdk://

添加上述网址,因为如果未安装Twitter应用,则twittersdk会尝试在网络视图中打开,因此我们必须添加此回调网址

2)在onCreate的SetContentView方法之前添加了此代码

//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);