Skip to content Skip to sidebar Skip to footer

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. I've followed the tutorial exactly (at least I think I have, maybe I've made some mi

Solution 1:

The Fabric SDK separates functionality into modules called Kits. You must indicate which kits you wish to use via Fabric.with(). This is typically done by extending Android’s Application class.

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

publicclassMyApplicationextendsApplication {
    @OverridepublicvoidonCreate() {
        super.onCreate();

        TwitterAuthConfigauthConfig=newTwitterAuthConfig("consumerKey",
                                         "consumerSecret");

        Fabric.with(this, newTwitter(authConfig));

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

More info: https://dev.twitter.com/twitter-kit/android/integrate

See the canonical sample app at: https://github.com/twitterdev/cannonball-android

Solution 2:

My case error is: Must start with Fabric.with() before calling twitter kit

Answer :

Before that I have used: Fabric.with(this, new Crashlytics()); & Fabric.with(this, new Twitter(authConfig)); Finally not working.

Before Integrating Twitter my code is

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

After Integrating Twitter I replace with

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

Now working like a charm,

Solution 3:

Here's how I implemented Twitter login with fabric:

  1. Declare twitter key and secret:

    privatestaticfinalStringTWITTER_KEY="r5nPFPbcDrzoJM9bIBCqyfHPK";
     privatestaticfinalStringTWITTER_SECRET="oJ8y2KPIySPpoBX3eCcqgcnmPGXLI94BR4g9ZztnApSmXQG9Ij ";
    
     //Twitter Login Button
     TwitterLoginButton twitterLoginButton;
    
  2. onCreate() method:

    //Initializing TwitterAuthConfig, these two line will also added automatically while configuration we didTwitterAuthConfig authConfig = newTwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
    Fabric.with(this, newTwitter(authConfig));
    
    setContentView(R.layout.activity_main);
    
    //Initializing twitter login button
    twitterLoginButton = (TwitterLoginButton) findViewById(R.id.twitterLogin);
    
    //Adding callback to the button
    twitterLoginButton.setCallback(newCallback<TwitterSession>() {
        @Overridepublicvoidsuccess(Result<TwitterSession> result) {
            //If login succeeds passing the Calling the login method and passing Result objectlogin(result);
        }
    
        @Overridepublicvoidfailure(TwitterException exception) {
            //If failure occurs while login handle it hereLog.d("TwitterKit", "Login with Twitter failure", exception);
        }
    });
    

3.override onActivityResult() :

@OverrideprotectedvoidonActivityResult(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():

publicvoidlogin(Result<TwitterSession> result) {

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

        //Getting the username from sessionfinalStringusername= 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, newCallback<User>() {
                    @Overridepublicvoidfailure(TwitterException e) {
                        //If any error occurs handle it here
                    }

                    @Overridepublicvoidsuccess(Result<User> userResult) {
                        //If it succeeds creating a User object from userResult.dataUseruser= userResult.data;

                        //Getting the profile image urlStringprofileImage= 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.

Solution 4:

Latest Twitter Integration with Android Studio

This below link provide the sample code you can use this code to integrate twitter latest sdk (Fabric). it's provide all feature we can easily integrate less time take

Twitter Sample code

Reference Code Plz check it

Post a Comment for "Twitter Fabric Login For Android"