Android的Twitter结构登录

Android的Twitter结构登录,android,twitter,twitter-fabric,Android,Twitter,Twitter Fabric,我正在尝试使用Twitter提供的新Fabric API让用户登录到我的应用程序。在完成了所有必要的步骤之后,我完全按照教程进行了设置(至少我认为我已经这样做了,也许我犯了一些错误);现在,当我点击登录按钮并进行身份验证时,按钮会返回一个成功的响应,但当我在之后去获取Twitter会话时,我会得到一个异常,如下所示 Caused by: java.lang.IllegalStateException: Must start Twitter Kit with Fabric.with() first

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

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

(同样,到目前为止,我一直遵循教程,但如果你能想到什么,我愿意尝试一下)

Fabric SDK将功能划分为称为工具包的模块。您必须通过Fabric.with()指明要使用的套件。这通常是通过扩展Android的应用程序类来实现的

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());
    }
}
更多信息:


查看规范示例应用程序,网址为:

最新Twitter与Android Studio集成

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


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

解决方案:

在此之前,我使用过: Fabric.with(这是新的Crashlytics());& Fabric.with(this,newtwitter(authConfig)) 终于不行了

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

--Fabric.with(this,newcrashlytics())

集成Twitter后,我将其替换为

--Fabric.with(this,newtwitter(authConfig),newcrashlytics())


现在,我的工作很有魅力,

以下是我如何使用fabric实现Twitter登录:

  • 声明twitter密钥和机密:

     private static final String TWITTER_KEY = "r5nPFPbcDrzoJM9bIBCqyfHPK";
     private static final String TWITTER_SECRET = "oJ8y2KPIySPpoBX3eCcqgcnmPGXLI94BR4g9ZztnApSmXQG9Ij ";
    
     //Twitter Login Button
     TwitterLoginButton twitterLoginButton;
    
  • 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);
        }
    });
    
    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();
    
                        }
                    });
        }
    
    公共作废登录(结果){
    //使用结果数据创建twitter会话
    TwitterSession=result.data;
    //正在从会话获取用户名
    最终字符串username=session.getUserName();
    //此代码将获取配置文件图像URL
    //正在获取登录用户的帐户服务
    getApiClient(会话).getAccountService()
    .verifyCredentials(真、假、新回调(){
    @凌驾
    公共无效失败(Twittere异常){
    //如果发生任何错误,请在此处处理
    }
    @凌驾
    public void成功(Result userResult){
    //如果它成功地从userResult.data创建用户对象
    User=userResult.data;
    //获取配置文件图像url
    字符串profileImage=user.profileImageUrl.replace(“_normal”,”);
    Log.d(“完成”,“名称-->”+用户名+“url-->”+档案图像);
    //Toast.makeText(这是“name-->”+用户名+“url-->”+profileImage,Toast.LENGTH_LONG).show();
    }
    });
    }
    

    您在
    login()
    中有用户名和profilepicture url,可以在任何地方使用。

    嗨,路易斯,您能帮我解答一下我的问题吗。。。提前谢谢,谢谢!我使用了Android Studio auto Fabric集成,完全错过了这段代码。我需要更改客户端的API密钥和密码,但不知道在哪里可以找到。请注意,如果使用twitter和crashlytics,则不能有两个不同的Fabric.with(…)语句。你必须将它们链接在一起:Fabric.with(…)。with(…)@Cipriani你能发布你第一个链接的更新版本吗?它似乎因“拒绝访问”重定向而关闭。您好,我在使用Fabric twitter登录工具包时遇到一些问题。从Android Studio Farbic的插件中,在选择安装工具包的屏幕上,选项是“未知”而不是“安装”。还有其他人遇到过这个问题吗?我必须提到,对于一个从头开始的项目,这是不会发生的。签出以下链接:您能告诉我,如果安装了应用程序,我如何将用户重定向到该应用程序的twitter登录屏幕(如果他未登录)或浏览器?TwitterAuthConfig authConfig=new TwitterAuthConfig(getResources().getString(R.string.TWITTER_KEY)、getResources().getString(R.string.TWITTER_SECRET))、Fabric.with(this、newtwitter(authConfig)、newcrashlytics());
    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();
    
                        }
                    });
        }