Twitter Fabric 登录 Android

13

我正在尝试使用Twitter提供的新Fabric API让用户登录我的应用程序。在完成所有必要步骤设置项目之后,我按照此处的教程精确地操作(至少我认为是这样,也许有些错误)。现在当我点击登录按钮并验证后,该按钮返回成功响应,但在那之后当我获取Twitter会话时,出现了一个异常,看起来像:

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

(再次强调,我到目前为止都完全按照教程操作,但如果你有什么想法,我愿意尝试)


请查看以下链接:https://androidbeasts.wordpress.com/2015/07/22/twitter-fabric-integration/ - Aakash
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


嗨,路易斯,你能帮我回答一下我的问题吗? 这里 ... 先谢谢啦。 - Yash Sampat
非常感谢!我使用了Android Studio自动Fabric集成,完全错过了这个小代码片段。我需要为客户更改我的API密钥和秘密,但不知道在哪里找到它。 - LukeWaggoner
6
请注意,如果您同时使用Twitter和Crashlytics,则不能使用2个不同的"Fabric.with(...)"语句。您必须将它们链接在一起:"Fabric.with(...).with(...)"。 - Greg Ennis
@Cipriani,你能否发布一份更新后的第一个链接?它似乎被重定向到“拒绝访问”。 - rob

5

我的错误提示是:必须在调用Twitter Kit之前使用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());

现在完美运行了。


TwitterAuthConfig authConfig = new TwitterAuthConfig(getResources().getString(R.string.TWITTER_KEY), getResources().getString(R.string.TWITTER_SECRET));Fabric.with(this, new Twitter(authConfig),new Crashlytics()); - Senghani Maulik

3

这是我如何使用fabric实现Twitter登录的方法:

  1. Declare twitter key and secret:

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

    //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.最后,login()函数:

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

                    }
                });
    }

login()中,您可以获得用户名和个人资料图片的URL,并在需要时使用它们。


请问您能告诉我如何将用户重定向到应用程序的 Twitter 登录界面(如果用户未登录),或者重定向到浏览器吗? - Ne AS

2

最新的Twitter集成与Android Studio

以下链接提供了示例代码,您可以使用此代码来集成Twitter的最新SDK(Fabric)。它提供了所有功能,我们可以轻松地在较短的时间内进行集成。

Twitter示例代码

参考代码,请查看


1
你好, 我在使用Fabric Twitter登录套件时遇到了一些问题。从Android Studio Farbic的插件中,在选择要安装的套件的屏幕上,选项显示为“未知”,而不是“安装”。还有其他人遇到这个问题吗? 值得一提的是,对于一个全新的项目,这种情况是不会发生的。 - Ionut Negru

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接