简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM