New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for getting email address from Twitter user #230

Merged
merged 2 commits into from Apr 24, 2016

Conversation

Projects
None yet
2 participants
@alibitek
Contributor

alibitek commented Feb 4, 2016

I propose a better approach than #220 for getting the email address of a Twitter user using the Twitter REST API endpoint GET account/verify_credentials

Because requesting a user's email address requires that Twitter whitelists the application the user signs into I made the inclusion of the include_email parameter configurable through either:

  1. twitter4j.properties file, by setting the includeEmail property to true
  2. ConfigurationBuilder , by calling the setIncludeEmailEnabled(true) method on it
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(cKey);
builder.setOAuthConsumerSecret(cSecret);
builder.setOAuthAccessToken(accessToken.getToken());
builder.setOAuthAccessTokenSecret(accessToken.getTokenSecret());
builder.setIncludeEmailEnabled(true);

Twitter twitter = new TwitterFactory(builder.build()).getInstance();
try {
    User user = twitter.verifyCredentials();
    if (user != null)
    {
        mUserName = user.getName();
        mUserNickName = user.getScreenName();
        mUserId = String.valueOf(user.getId());
        mEmail = user.getEmail();

        Log.debug(TwitterManager.class, "TwitterManager.VerifyCredentials(): user_name = " + mUserName +
                ", user_nick_name = " + mUserNickName +
                ", user_id = " + mUserId +
                ", user_email = " + mEmail);
    }
} catch (TwitterException e) {
        Log.error(TwitterManager.class , "TwitterManager.VerifyCredentials error: " + e.getMessage());
        e.printStackTrace();
}
  • The includeEmailEnabled parameter in ConfigurationBase is false by default because the email is only included in the response after a Twitter application has been whitelisted, thus it should only be sent to Twitter explicitly by each individual application.
  • The commit about ensuring null is returned from ParseUtils.getRawString has been added because a user of twitter4j library can set includeEmail to true, but his application is not whitelisted, therefore there is no email returned in the JSON response and JsonObject.getString method raises NullPointerException, and if we catch JsonException the exception will only be handled upstream in the UserJSONImpl::init method, which throws it further upstream. This is not what we want.
    I've used catch Exception class because JsonObject.getString can also raise ClassCastException, see here for a test case

Alex Butum added some commits Feb 4, 2016

Alex Butum
Ensure null is returned from JsonObject.getString(String name) if:
- the specified name doesn't have any mapping
- the value for specified name mapping is not assignable to JsonString

@yusuke yusuke merged commit c8c2a75 into Twitter4J:master Apr 24, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment