3

i am using twitter integration using fabric, now issue is i am able to get all the details of user except email address. following is my code, can any one help me with that

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
        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);
                    System.out.println("Twitter Email"+user.email);
                    //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();
                }


            }

        });
    }
chris
  • 699
  • 4
  • 12
  • 35

4 Answers4

7

This is how I fetched the user email of an user:

final TwitterSession twitterSession = result.data;
twitterAuthClient.requestEmail(twitterSession, new com.twitter.sdk.android.core.Callback<String>() {
    @Override
    public void success(Result<String> emailResult) {
        String email = emailResult.data;
        // ...
    }

    @Override
    public void failure(TwitterException e) {
        callback.onTwitterSignInFailed(e);
    }
});

So you have to call TwitterAuthClient.requestEmail() once you got a successful Result<TwitterSession> upon authorization.

Be aware that you'll have to contact Twitter support to enable access to users' email for your app. An error message with this will show up.

Eduard B.
  • 6,735
  • 4
  • 27
  • 38
  • app crash it gives java.lang.RuntimeException: Failure delivering result ResultInfo – chris Mar 14 '17 at 10:27
  • what is twitterAuthClient?? – chris Mar 14 '17 at 10:28
  • `twitterAuthClient` is a `TwitterAuthClient`. I guess that you already use one for the initial authorization. Can you post the whole stacktrace? Only the first line doesn't say much. – Eduard B. Mar 14 '17 at 10:32
  • It basically says that your `TwitterAuthClient` is null, so you should make sure that you have it instantiated. – Eduard B. Mar 14 '17 at 10:54
  • neither beginner nor trolling..i used your answer..and check here https://docs.fabric.io/android/twitter/log-in-with-twitter.html too..and already found my mistake..but pasted commet later..haha – chris Mar 14 '17 at 10:59
3

Here is my code to get details from twitter:

   private void intializeTwitterUI() {
      loginButton = (TwitterLoginButton)             
      findViewById(R.id.twitter_login_button);
      loginButton.setCallback(new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) {
            // The TwitterSession is also available through:
            // TWITTER.getInstance().core.getSessionManager().getActiveSession()
            TwitterSession session = result.data;
            // TODO: Remove toast and use the TwitterSession's userID
            // with your app's user model
            String msg = "Twitter: @" + session.getUserName() + " logged in! (#" + session.getUserId() + ")";
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            /**
             *
             */
            AccountService _AccountService = Twitter.getApiClient(result.data).getAccountService();
            _AccountService.verifyCredentials(true, true).enqueue(new retrofit2.Callback<User>() {
                @Override
                public void onResponse(Call<User> call, retrofit2.Response<User> response) {
                    Log.d(TAG, "Twitter user is: " + response.toString());
                    Log.d(TAG, "Twitter-Email" + response.body().email);
                    Log.d(TAG, "Twitter-profileImage" + response.body().profileImageUrl);
                    Log.d(TAG, "Twitter-ID" + response.body().id);
                    twitterDetails = response.body().email + "," + response.body().profileImageUrl + "," + response.body().id;

                }

                @Override
                public void onFailure(Call<User> call, Throwable t) {
                    Log.e(TAG, "verifyCredentials failed! " + t.getLocalizedMessage());
                }
            });



        }
Sandeep Kharat
  • 500
  • 1
  • 4
  • 12
  • you need to modify your code accordingly. And please go through this link https://docs.fabric.io/android/twitter/log-in-with-twitter.html – Sandeep Kharat Mar 14 '17 at 10:39
2

For those that want to do it with Kotlin can try in this way:

    val session = TwitterCore.getInstance().sessionManager.activeSession as TwitterSession
    val authClient = TwitterAuthClient()
    authClient.requestEmail(session, object : Callback<String>(){
            override fun failure(exception: TwitterException?) {
                email.setText("Welcome to Twitter")
            }

            override fun success(result: Result<String>?) {
                email.setText("Welcome " + result?.data)
            }

        })
Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
0

It is possible to request an email address from users, but it requires your app to be whitelisted. check here Is there a way to get an user's email ID after verifying his/her Twitter identity using OAuth?

Community
  • 1
  • 1