How can i get email address of a user via twitter API? I\'m using Twitter4j for Sign in with twitter
first of all make sure you activated Request email addresses from users
in your twitter app Permissions
section
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);
}
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
}
});
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.
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());
}
});
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);
}
});
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) {
}
});