Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get an email address from users in 'Twitter4J'?

I'm glad that now we can get an email address from users now with 'Twitter4J'.

But, my problem is that I can't use the function which is bring an email from users. My server (based on Spring) had been using Twitter4J with 'maven dependency'. I'm using the way that described in Twitter4J's homepage(http://twitter4j.org/en/index.html):

<dependencies>
  <dependency>
       <groupId>org.twitter4j</groupId>
       <artifactId>twitter4j-core</artifactId>
       <version>[4.0,)</version>
   </dependency>
   ...
</dependencies>

However, this way can not bring your latest function which is bring an email even use SNAPSHOT Build Version'. I think this way can not bring the latest version of Twitter4J which was upload recently in github.

How can I use getEmail() function using Twitter4J in my application?

Any help is much appreciated, thanks in advance.

like image 280
YeonSeok Choi Avatar asked Feb 24 '16 08:02

YeonSeok Choi


1 Answers

First, you need to get your application whitelisted, from the documentation:

Requesting a user’s email address requires your application to be whitelisted by Twitter.

If you get the permission You need to set up the ConfigurationBuilder wtih setIncludeEmailEnabled(true)

ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(cKey);
builder.setOAuthConsumerSecret(cSecret);
builder.setOAuthAccessToken(accessToken.getToken());
builder.setOAuthAccessTokenSecret(accessToken.getTokenSecret());
builder.setIncludeEmailEnabled(true); 

And then you can get the user mail after verifying the credentials

User user = twitter.verifyCredentials();
System.out.print(user.getEmail());

As Patrick Denny points out, this method is live at Oct 4th 2016 in Twitter4j 4.0.5

So, if you have a older version and need to get the email, please upgrade

like image 108
FeanDoe Avatar answered Nov 07 '22 05:11

FeanDoe