13

我正在尝试使用 Twitter 提供的新 Fabric API 让用户登录到我的应用程序。在使用所有必要步骤设置我的项目之后,我已经完全按照教程(至少我认为我有,也许我犯了一些错误)在这里; 现在,当我点击登录按钮并验证按钮时,会返回一个成功的响应,但是当我之后去获取 Twitter 会话时,我得到一个看起来像的异常

Caused by: java.lang.IllegalStateException: Must start Twitter Kit with Fabric.with() first    

(同样,到目前为止,我一直按照教程进行操作,但是如果您能想到任何东西,那么我愿意尝试)

4

4 回答 4

15

Fabric SDK 将功能分成称为 Kits 的模块。您必须通过 Fabric.with() 指明您希望使用的套件。这通常通过扩展 Android 的 Application 类来完成。

package com.example.app;
import android.app.Application;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        TwitterAuthConfig authConfig = 
                   new TwitterAuthConfig("consumerKey",
                                         "consumerSecret");

        Fabric.with(this, new Twitter(authConfig));

        // Example: multiple kits
        // Fabric.with(this, new Twitter(authConfig),
        //                  new Crashlytics());
    }
}

更多信息:https ://dev.twitter.com/twitter-kit/android/integrate

请参阅规范示例应用程序:https ://github.com/twitterdev/cannonball-android

于 2014-10-29T17:01:02.470 回答
5

我的案例错误是:在调用 twitter 工具包之前必须从 Fabric.with() 开始

解决方案:

在此之前我使用过: Fabric.with(this, new Crashlytics()); & Fabric.with(this, new Twitter(authConfig)); 终于不行了。

在集成 Twitter 之前,我的代码是

-- Fabric.with(this, new Crashlytics());

集成 Twitter 后,我替换为

-- Fabric.with(this, new Twitter(authConfig),new Crashlytics());

现在像魅力一样工作,

于 2016-03-15T10:36:04.763 回答
3

以下是我使用 Fabric 实现 Twitter 登录的方法:

  1. 声明 twitter 密钥和秘密:

     private static final String TWITTER_KEY = "r5nPFPbcDrzoJM9bIBCqyfHPK";
     private static final String TWITTER_SECRET = "oJ8y2KPIySPpoBX3eCcqgcnmPGXLI94BR4g9ZztnApSmXQG9Ij ";
    
     //Twitter Login Button
     TwitterLoginButton twitterLoginButton;
    
  2. onCreate()方法:

    //Initializing TwitterAuthConfig, these two line will also added automatically while configuration we did
    TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
    Fabric.with(this, new Twitter(authConfig));
    
    setContentView(R.layout.activity_main);
    
    //Initializing twitter login button
    twitterLoginButton = (TwitterLoginButton) findViewById(R.id.twitterLogin);
    
    //Adding callback to the button
    twitterLoginButton.setCallback(new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) {
            //If login succeeds passing the Calling the login method and passing Result object
            login(result);
        }
    
        @Override
        public void failure(TwitterException exception) {
            //If failure occurs while login handle it here
            Log.d("TwitterKit", "Login with Twitter failure", exception);
        }
    });
    

3.覆盖onActivityResult()

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //Adding the login result back to the button
        twitterLoginButton.onActivityResult(requestCode, resultCode, data);
    }

4.最后,登录()

public void login(Result<TwitterSession> result) {

//Creating a twitter session with result's data
        TwitterSession session = result.data;

        //Getting the username from session
        final String username = session.getUserName();

        //This code will fetch the profile image URL
        //Getting the account service of the user logged in
        Twitter.getApiClient(session).getAccountService()
                .verifyCredentials(true, false, new Callback<User>() {
                    @Override
                    public void failure(TwitterException e) {
                        //If any error occurs handle it here
                    }

                    @Override
                    public void success(Result<User> userResult) {
                        //If it succeeds creating a User object from userResult.data
                        User user = userResult.data;

                        //Getting the profile image url
                        String profileImage = user.profileImageUrl.replace("_normal", "");

                        Log.d("done","name-->"+username + "url-->"+profileImage);
                       // Toast.makeText(this,"name-->"+username + "url-->"+profileImage,Toast.LENGTH_LONG).show();

                    }
                });
    }

您有用户名和个人资料图片 url 可以在login()任何地方使用。

于 2016-06-07T11:28:23.977 回答
2

与 Android Studio 的最新 Twitter 集成

以下链接提供了示例代码,您可以使用此代码集成 twitter 最新 sdk (Fabric)。它提供了我们可以轻松集成的所有功能,花费更少的时间

推特示例代码

参考代码请检查它

于 2015-01-06T06:16:49.017 回答