Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user data through twitter's Fabric API (Android)

I've successfully integrated twitter's fabric api in my project A twitter login button

<com.twitter.sdk.android.core.identity.TwitterLoginButton
                        android:id="@+id/twitter_login_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"/>

A manifest permission

  <meta-data
            android:name="io.fabric.ApiKey"
            android:value="myfabrickey" />

Initialization

 //field declerations
         private static final String TWITTER_KEY = "yourtwkey";
            private static final String TWITTER_SECRET = "yoursecret";

    //Inside oncreate
          TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
                Fabric.with(this, new Crashlytics(), new Twitter(authConfig));
  loginButtonTwitter = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
        loginButtonTwitter.setCallback(new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                // Do something with result, which provides a TwitterSession for making API calls
            }

            @Override
            public void failure(TwitterException exception) {
                // Do something on failure
            }
        });

//Inside onActivityResult
 loginButtonTwitter.onActivityResult(requestCode, resultCode, data);

My only question is, how do i get the user data in a json format once i authenticate my account.

like image 743
Abhilash Avatar asked Jun 01 '15 02:06

Abhilash


2 Answers

Ok! It was quite simple. (was trying to do it during midnight so i guess half of my brain was asleep :P) Referenced from this link https://twittercommunity.com/t/android-get-user-profile-image/30579/6 In the user object, you'll get all the stuff.

  TwitterSession session =
                Twitter.getSessionManager().getActiveSession();
        Twitter.getApiClient(session).getAccountService()
                .verifyCredentials(true, false, new Callback<User>() {



                    @Override
                    public void success(Result<User> userResult) {

                        User user = userResult.data;
                        twitterImage = user.profileImageUrl;

                    }
                    @Override
                    public void failure(TwitterException e) {

                    }

                });
like image 97
Abhilash Avatar answered Sep 21 '22 11:09

Abhilash


For those who getting error "verifyCredentials() in AccountService cannot be applied to .. " Please change the code like below

    Call<User> userResult = Twitter.getApiClient(session).getAccountService().verifyCredentials(true,false);
            userResult.enqueue(new Callback<User>() {

                @Override 
                public void failure(TwitterException e) { 

                } 

                @Override 
                public void success(Result<User> userResult) { 

                    User user = userResult.data; 
                    String twitterImage = user.profileImageUrl; 

                    try { 
                        Log.d("imageurl", user.profileImageUrl); 
                        Log.d("name", user.name); 
                        Log.d("email",user.email); 
                        Log.d("des", user.description); 
                        Log.d("followers ", String.valueOf(user.followersCount)); 
                        Log.d("createdAt", user.createdAt); 
                    } catch (Exception e) { 
                        e.printStackTrace(); 
                    } 


                } 

            }); 
like image 29
AmAnDroid Avatar answered Sep 21 '22 11:09

AmAnDroid