3

How can i get email address of a user via twitter API? I'm using Twitter4j for Sign in with twitter

LEE
  • 3,335
  • 8
  • 40
  • 70
  • possible duplicate of [Email of Twitter User in Twitter4j](http://stackoverflow.com/questions/11083145/email-of-twitter-user-in-twitter4j) – Daniel Nugent Mar 19 '15 at 05:51
  • 1
    Also, see this link: https://dev.twitter.com/twitter-kit/android/request-email – Daniel Nugent Mar 19 '15 at 06:10
  • @DanielNugent..your link is helpful but which method should i use in case of twitter4j? – LEE Mar 19 '15 at 06:18
  • Also, for the first link it gives me XML doc error when i enter my username @DanielNugent – LEE Mar 19 '15 at 06:24
  • Take a look at this: https://twittercommunity.com/t/easy-way-of-get-users-twitterid-and-email/17099 – Daniel Nugent Mar 19 '15 at 06:24
  • @DanielNugent...yes there is a way using class TwitterAuthClient. However, that's what the problem is ! I'm not using the Fabric and Core Twitter API because i wasn't able to successfully import its library in my gradle build file. Hence i switched to twitter4j. But for twitter4j there is no way to get email !!! Weird :\ – LEE Mar 19 '15 at 06:41
  • do you have a session? login to your app by twitter, if not then you cant get the email adress. – Mounir Elfassi Mar 20 '15 at 10:16

5 Answers5

0

I hope this will be work please Use following code in your request call back result

 TwitterloginButton.setCallback(new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                session = result.data;

                AccountService ac = Twitter.getApiClient(result.data).getAccountService();
                ac.verifyCredentials(true, true, new Callback<User>() {
                    @Override
                    public void success(Result<com.twitter.sdk.android.core.models.User> result) {

                        String imageUrl = result.data.profileImageUrl;
                        String email = result.data.email;
                        String Name = result.data.name;
                        long userid = result.data.id;
                        String username = result.data.screenName;

                        System.out.println(imageUrl);
                        System.out.println("EMAIL:" + email);
                        System.out.println("Name:" + Name);
                        System.out.println("ID:" + userid);
                        System.out.println("Username:" + username);
                    }

                    @Override
                    public void failure(TwitterException e) {

                    }
                });
Dashrath Rathod
  • 508
  • 1
  • 5
  • 15
0

check these links

https://dev.twitter.com/twitterkit/android/installation

https://dev.twitter.com/twitterkit/android/log-in-with-twitter

Write below code after successful login

TwitterAuthClient authClient = new TwitterAuthClient();
authClient.requestEmail(session, new Callback<String>() {
   @Override
    public void success(Result<String> result) {
    // Do something with the result, which provides the email address
}

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

first of all make sure you activated Request email addresses from users in your twitter app Permissions section

enter image description here


then follow Twitter Documentation steps:

1. Installation process: which simply imports the sdk package of twitter

and i'm using only the core package

compile 'com.twitter.sdk.android:twitter-core:3.1.1'

Initialize Twitter Kit on your Activity onCreate method

Twitter.initialize(this);

Add your App id and secret to strings.xml file

<string android:name="com.twitter.sdk.android.CONSUMER_KEY">XXXXXXXXXXX</string>
<string android:name="com.twitter.sdk.android.CONSUMER_SECRET">XXXXXXXXXXX</string>

2. Add the Button:

<com.twitter.sdk.android.core.identity.TwitterLoginButton
     android:id="@+id/twitter_connect"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
HINT: you can use a custom button just follow this simple answer
private TwitterLoginButton twitterBtn;

...

twitterBtn = (TwitterLoginButton) findViewById(R.id.twitter_connect);
twitterBtn.setCallback(new Callback<TwitterSession>() {
    @Override
    public void success(Result<TwitterSession> result) {
        // Do something with result, which provides a TwitterSession for making API calls
        // which is do the callback from twitter to get the Email
        TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
        TwitterAuthToken authToken = session.getAuthToken();
        String token = authToken.token;
        String secret = authToken.secret;

        TwitterAuthClient authClient = new TwitterAuthClient();
        authClient.requestEmail(session, new Callback<String>() {
            @Override
            public void success(Result<String> result) {
                // Do something with the result, which provides the email address
                // the email is saved in the result variable 'result.data'
                Toast.makeText(getBaseContext(), "Email" + result.data, Toast.LENGTH_LONG).show();
            }

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

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

Next, pass the result of the authentication Activity back to the button:

// Pass the activity result to the login button.
twitterBtn.onActivityResult(requestCode, resultCode, data);

like so:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Pass the activity result to the login button.
    twitterBtn.onActivityResult(requestCode, resultCode, data);
}
Biskrem Muhammad
  • 4,074
  • 3
  • 31
  • 38
0

Here Twitter sdk helps you to provide the email, You just need to provide the permission requesting for email. Twitter sdk will check for the visibility of the email on your twitter account,If it has the permission it will provide you the Email else we need to handle the if it doesn't send's the email.

client.requestEmail(twitterSession, new Callback<String>() {
        @Override
        public void success(Result<String> result) {
            Log.d(TAG, "Twitter success email : " + result.data);
            email = result.data;
            if (email == null){

            }else {
            Log.d(TAG,"Email"+ email);  
            }

        }

        @Override
        public void failure(TwitterException exception) {
            Log.d(TAG, "Twitter failed  email : " + exception);
        }
    });
  • TwitterAuthClient client = new TwitterAuthClient(); – rohit panhalkar Mar 16 '18 at 14:08
  • 2
    plz add little explanation too – ShivaGaire Mar 16 '18 at 14:27
  • While this code may answer the question, providing additional context regarding **how** and **why** it solves the problem would improve the answer's long-term value. – Alexander Mar 16 '18 at 17:52
  • Here Twitter sdk helps you to provide the email, You just need to provide the permission requesting for email. Twitter sdk will check for the visibility of the email on your twitter account,If it has the permission it will provide you the Email else we need to handle the if it doesn't send's the email. – rohit panhalkar Apr 09 '18 at 06:46
0

To Get Twitter Email and Profile Image URL , username, id you to call requestEmail method.

Also You to allow request email in your developer account of twitter. See below images.

Retrieve Email id on twitter login

Use Below code to Get user details.

    btnTwitterLogin.setCallback(new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) {
            TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
            TwitterAuthToken authToken = session.getAuthToken();
            TwitterAuthClient authClient = new TwitterAuthClient();
            authClient.requestEmail(session, new Callback<String>() {
                @Override
                public void success(Result<String> result) {
                    String email =result.data;
                    String token = authToken.token;
                    String secret = authToken.secret;
                    String username = session.getUserName();
                    long userId = session.getUserId();
                    Log.e("neem", "success: \n Token =>  " + token + "\n Secret: " + secret + " \n Username: " + username + "\n Userid: " + userId+" \n Email: "+email);

                }

                @Override
                public void failure(TwitterException exception) {
                    Log.e("neem", "failure: "+exception.getMessage() );
                }
            });
            TwitterCore.getInstance().getApiClient(result.data).getAccountService().verifyCredentials(false,true,false).enqueue(new Callback<User>() {
                @Override
                public void success(Result<User> userResult) {
                    try {

                        String imageUrl = userResult.data.profileImageUrl;
                        String username = userResult.data.screenName;
                        Log.e("neem", "acount services: imageurl: "+imageUrl+"\n username: "+username );

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void failure(TwitterException e) {
                }
            });

        }

        @Override
        public void failure(TwitterException exception) {
            ShowToast(mcontext, "Login failed due to: \n "+exception.getMessage());
        }
    });
Nimesh Patel
  • 1,394
  • 13
  • 26