57

How to get email id of the user who accepted my Twitter application?

I have gone through lot of forums. But they have mentioned, it is not possible. Also those posts are older than a year. May I know whether it is possible to get the user email id through twitter API using PHP?

I am getting Twitter user details using the following URL:

https://api.twitter.com/1.1/account/verify_credentials.json

Oluwafemi
  • 14,243
  • 11
  • 43
  • 59
Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
  • Here i will get a solution also for the problem mentioned by vendors. (i.e) how to handle the situation for that case.. – Vinoth Babu Mar 25 '14 at 06:49
  • I can't find it in the Twitter docs! Only found answers from the Twitter community forums: https://twittercommunity.com/t/twitter-oauth-after-connect-get-primary-email-address/1563/11 – jonprasetyo Oct 05 '14 at 16:02
  • 2
    @PaulDessert a member of this particular vendor's staff has stated on a public [forum](https://twittercommunity.com/t/how-to-get-email-from-twitter-user-using-oauthtokens/558/175) that they have implemented functionality without documenting it. It stands to reason then, that someone (like the developers of the functionality, for instance) know how to use the feature despite it not appearing in the API documentation at all. These same people might be able to offer someone like Vinoth help he couldn't find elsewhere. – jeteon Apr 09 '15 at 20:02
  • As of January 7, 2017 this repo I created works and shows you exactly how to extract the users email address. https://github.com/DZuz14/CompleteSignInWithTwitterPHP – Dan Zuzevich Jan 07 '17 at 16:59

6 Answers6

100

This is now possible by filling out a form to request elevated permissions:

  1. Go to https://support.twitter.com/forms/platform
  2. Select "I need access to special permissions"
  3. Enter Application Name and ID. These can be obtained via https://apps.twitter.com/ -- the application ID is the numeric part in the browser's address bar after you click your app.
  4. Permissions Request: "Email address"
  5. Submit & wait for response
  6. After your request is granted, an addition permission setting is added in your twitter app's "Permission" section. Go to "Additional Permissions" and just tick the checkbox for "Request email addresses from users".

Note that user e-mail address has to be verified, otherwise Twitter refuses to provide it. (See include_email parameter description in elevated permissions doc page.)

joseph11
  • 3
  • 2
aleemb
  • 31,265
  • 19
  • 98
  • 114
  • 1
    Do they actually grant this permission easily? Or is it just reserved for already successful apps? – anges244 Feb 26 '16 at 13:13
  • 1
    @anges244 It seems they do grant it easily, at least to a developer who has never had a complaint. My app isn't even accessible online yet, so they can't have looked at it. – tremby Feb 29 '16 at 21:39
  • 2
    @aleemb, once you have the permission, how do you get the email address? I authorized my test user with this extra permission, but still don't see the email address anywhere in the `users/show` response. Is there something which needs to be added to the request? – tremby Feb 29 '16 at 21:40
  • 1
    @tremby I am not sure but I think you have to make the call to accounts/verify_credentials (https://dev.twitter.com/rest/reference/get/account/verify_credentials) – anges244 Feb 29 '16 at 23:23
  • 1
    @anges244 -- found that in the mean time; just suggested an edit to the answer to clarify for others. Thanks. – tremby Feb 29 '16 at 23:24
  • 1
    (My edit was rejected; unclear why) After you have the elevated permissions, the email address will be available in the response to a [`account/verify_credentials`](https://dev.twitter.com/rest/reference/get/account/verify_credentials) call as long as it was made with the `include_email` option. It won't become available in a `users/show` response. – tremby Mar 01 '16 at 21:29
  • Bear in mind that even though an user has confirmed their account, it might not appear in `account/verify_credentials` with `include_email=true`. This happened to my account, which I had with twitter for some time. My guess is that users that registered before the update that allowed for email lookup are treated as unconfirmed. After changing my email it successfully popped up in the retrieved json. – eithed Apr 15 '16 at 08:23
  • 1
    I got my app whitelisted, and added the extra permission.When the twitter page is displayed to the user to confirm permissions, it says the app will be able to see the email address. I put the include_email=true parameter as a normal parameter on the url for the GET request, which looks like this: "...api.twitter.com/1.1/account/verify_credentials.json?include_email=1" . I got the user info okay, with the json including the correct id, name, etc, but no email address. I logged in to twitter and changed/confirmed a new email address, still not luck. Any ideas? – sootsnoot Jun 24 '16 at 00:03
  • 2
    FYI, I found my answer on Twitter Developer Forums (https://twittercommunity.com/t/email-not-being-returned-from-verify-credentials-yes-were-whitelisted/63443/2). In the PHP source I had specified the parameter list as array('include_email'=>true), which becomes include_email=1 in the url. I changed the source to array('include_email'=>'true'), and it started working. What a wonky API! The value of the preceding boolean parameter to this url, skip_status, is documented "When set to either true, t or 1 statuses will not...". But include_email says "When set to true email will be returned". – sootsnoot Jun 24 '16 at 15:45
  • This answer is a good start, but the person also asks how to implement it with PHP. I have created a working repo to showcase this. https://github.com/DZuz14/CompleteSignInWithTwitterPHP – Dan Zuzevich Jan 07 '17 at 17:04
  • Hi! After doing the same for email permission to get user email address, still not getting the same. What can be the possible reason? – Dhirender Jan 10 '17 at 06:31
  • 1
    Twitter updated their API. Now you can configure it in your app permission settings. Follow the next answers. – Arti Singh Nov 21 '18 at 12:52
43

Now you can fetch user email address from twitter API and it's a lot much easier. Just follow these steps...

  1. Goto Twitter Apps

  2. Click on 'Create New App'

  3. Fill all required credentials and click on 'Create your Twitter application'

  4. Now click on 'Permissions' tab -> check 'Request email addresses from users' field and click on 'Update Settings'. (check given picture)

  5. Now in your PHP code, set all app details and add this code!

    $params = array('include_email' => 'true', 'include_entities' => 'false', 'skip_status' => 'true');

    $data = $connection->get('account/verify_credentials', $params); // get the data

    // getting twitter user profile details $twt_id = $data->id; //twitter user id $twt_email = $data->email; //twitter user email

All Done. Hope it help you, good luck. ;)

enter image description here

Pran
  • 1,817
  • 23
  • 23
  • 2
    After looking for almost 2 hours, this was the solution. This should be heavily upvoted. It is January 1/7/2017 as of my typing this. None of the other ways of structuring the url worked anywhere I found on the internet. – Dan Zuzevich Jan 07 '17 at 15:22
  • 5
    After doing the same. still not getting email address. – Dhirender Jan 10 '17 at 06:27
  • not working for me either. any other ways? can i request for email address in development phase? – shrawan_lakhe Sep 04 '17 at 11:00
  • 1
    You will need to regenerate the access token to get the email from an already authenticated user. It's a security from Twitter. – Florian Doyen Jan 23 '18 at 09:04
  • If you have a Twitter user saved, you need to destroy their entry in your providers database table. The Twitter docs said that. But also scroll down through the fields returned by `$connection->get('account/verify_credentials', ['include_email' => true])`, because it's at the bottom. – agm1984 May 06 '20 at 07:46
  • Thanks, @agm1984 I just deleted the entry from the table and it worked for me – Mohit Rane Dec 17 '20 at 11:10
8

It is now possible to retrieve a user's email address from Twitter (if the user permits it, of course).

You'll need to apply to have your application white-listed to receive xAuth.

Check here for more info: https://dev.twitter.com/rest/reference/get/account/verify_credentials

Kevin Traas
  • 415
  • 4
  • 5
  • 1
    You should have mentioned "xAuth is not available for web-based applications". This is why your answer doesn't apply to the op's problem. – TNT Aug 30 '15 at 18:44
5

Yes you can get email address.This is now possible by filling out a some permission.

1.Go to this Link: https://apps.twitter.com/

2.After go to permission tab inside your created Application

3.Select Addition permission checkbox in your APP.

4.After successfully update setting you can get email address from Twitter.

If in some case you can not getting email address then please check your Oauth Api request url.

Your request url should be this type :https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true

Chirag Prajapati
  • 529
  • 6
  • 10
2

It is not possible to get user's email address from twitter. You can see it here. You can open a form page on your callback page and get user's email address on that page. You can refer here for example usage

Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
  • Huseyin BABAL: Thanks for your reply. May i know the link from dev.twitter.com where i can assure it. So i can show it to the client and go a head. – Vinoth Babu Mar 25 '14 at 06:41
  • Thanks Man. I will do as u told. – Vinoth Babu Mar 25 '14 at 06:43
  • 3
    Twitter FAQ link is broken :( – Asaph Feb 15 '15 at 07:39
  • 1
    It seems that this might be about to change [twittercommunity discussion](https://twittercommunity.com/t/how-to-get-email-from-twitter-user-using-oauthtokens/558/175). It seems its almost implemented but not yet officially documented. – jeteon Apr 09 '15 at 19:56
  • 1
    I think that this is about to change: https://twittercommunity.com/t/how-to-get-email-from-twitter-user-using-oauthtokens/558/175. – jeteon Apr 09 '15 at 20:03
1

Here is how I have done this in ASP.Net using linqtoTwitter library http://www.bigbrainintelligence.com/Post/get-users-email-address-from-twitter-oauth-ap

// call verify credentials api
var twitterCtx = new TwitterContext(authTwitter);
var verifyResponse =
      await
          (from acct in twitterCtx.Account
           where (acct.Type == AccountType.VerifyCredentials) && (acct.IncludeEmail == true)
           select acct)
          .SingleOrDefaultAsync();

if (verifyResponse != null && verifyResponse.User != null)
{
    User twitterUser = verifyResponse.User;
    //assign email to existing authentication object
    loginInfo.Email = twitterUser.Email;

}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • What was the credentialstore that you used? I tried their code sample but only get back "my" user account info - not the user info of the user that just logged in. – Tim Wheeler Sep 15 '17 at 03:27