简体   繁体   English

Twitter Fabric登录Android版

[英]Twitter Fabric Login for Android

I'm attempting to use the new Fabric API that Twitter is offering to let users login to my app. 我正在尝试使用Twitter提供的新Fabric API来让用户登录我的应用程序。 I've followed the tutorial exactly (at least I think I have, maybe I've made some mistakes) here after setting up my project with all of the necessary steps; 完成了包含所有必要步骤的项目之后,我完全按照教程(至少我认为我有,也许我犯了一些错误)。 now when I hit the login button and authenticate the button gives back a successful response but when I go to get the Twitter Session after that I get an exception that looks like 现在,当我点击登录按钮并验证按钮时,会给出成功的响应,但是当我去获取Twitter会话后,我得到一个看起来像

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

(again, I followed the tutorial to a T up to this point, but if you can think of anything then I'd be willing to try it) (再次,我按照教程一直到T,但如果你能想到任何事情,那么我愿意尝试一下)

The Fabric SDK separates functionality into modules called Kits. Fabric SDK将功能分为称为Kits的模块。 You must indicate which kits you wish to use via Fabric.with(). 您必须通过Fabric.with()指明您希望使用哪些套件。 This is typically done by extending Android's Application class. 这通常通过扩展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());
    }
}

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

See the canonical sample app at: https://github.com/twitterdev/cannonball-android 请参阅规范示例应用程序: https//github.com/twitterdev/cannonball-android

My case error is: Must start with Fabric.with() before calling twitter kit 我的案例错误是: 在调用twitter工具包之前必须先从Fabric.with()开始

Solution: 解:

Before that I have used: Fabric.with(this, new Crashlytics()); 在此之前我使用过: Fabric.with(这个,新的Crashlytics()); & Fabric.with(this, new Twitter(authConfig)); &Fabric.with(this,new Twitter(authConfig)); Finally not working. 终于不行了。

Before Integrating Twitter my code is 在整合Twitter之前我的代码是

-- Fabric.with(this, new Crashlytics()); - Fabric.with(这个,新的Crashlytics());

After Integrating Twitter I replace with 在集成Twitter之后我替换为

-- Fabric.with(this, new Twitter(authConfig),new Crashlytics()); - Fabric.with(这个,新的Twitter(authConfig),新的Crashlytics());

Now working like a charm, 现在像魅力一样工作

Here's how I implemented Twitter login with fabric: 以下是我使用fabric实现Twitter登录的方法:

  1. Declare twitter key and secret: 声明twitter密钥和秘密:

      private static final String TWITTER_KEY = "r5nPFPbcDrzoJM9bIBCqyfHPK"; private static final String TWITTER_SECRET = "oJ8y2KPIySPpoBX3eCcqgcnmPGXLI94BR4g9ZztnApSmXQG9Ij "; //Twitter Login Button TwitterLoginButton twitterLoginButton; 
  2. onCreate() method: 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.override onActivityResult() : 3.override 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.finally, login() : 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();

                    }
                });
    }

You have username and profilepicture url in login() to use wherever you want. 您可以在login()中使用用户名和profilepicture网址,以便随时随地使用。

Latest Twitter Integration with Android Studio 与Android Studio的最新Twitter集成

This below link provide the sample code you can use this code to integrate twitter latest sdk (Fabric). 下面的链接提供了示例代码,您可以使用此代码来集成twitter最新的sdk(Fabric)。 it's provide all feature we can easily integrate less time take 它提供了我们可以轻松集成所有功能的更少时间

Twitter Sample code Twitter示例代码

Reference Code Plz check it 参考代码Plz检查它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM