5

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.

Alex Bitek
  • 6,529
  • 5
  • 47
  • 77

1 Answers1

6

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

Community
  • 1
  • 1
FeanDoe
  • 1,608
  • 1
  • 18
  • 30
  • I had request to Twitter and my application got permission that can get email address through this way - http://stackoverflow.com/a/32852370/5973095 – YeonSeok Choi Feb 25 '16 at 00:39
  • 1
    For anyone reading this on or after Oct 4th 2016, this is in Twitter4j 4.0.5 I had to get the jars from https://mvnrepository.com/artifact/org.twitter4j as the official documentation wasn't updated yet, but I assume that it will shortly. – Patrick Denny Oct 04 '16 at 18:02