Can we get email ID from Twitter oauth API?
Asked Answered
I

6

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

Igorot answered 25/3, 2014 at 6:37 Comment(5)
Here i will get a solution also for the problem mentioned by vendors. (i.e) how to handle the situation for that case..Igorot
I can't find it in the Twitter docs! Only found answers from the Twitter community forums: twittercommunity.com/t/…Roberson
@PaulDessert a member of this particular vendor's staff has stated on a public forum 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.Embattled
As of January 7, 2017 this repo I created works and shows you exactly how to extract the users email address. github.com/DZuz14/CompleteSignInWithTwitterPHPEggert
Please check this comment github.com/firebase/FirebaseUI-Android/issues/… , I have get email after User authentication settings changes on twitter development portal.Fabliau
S
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.)

Sommers answered 29/9, 2015 at 19:36 Comment(12)
Do they actually grant this permission easily? Or is it just reserved for already successful apps?Schmit
@Schmit 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.Instance
@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?Instance
@Instance I am not sure but I think you have to make the call to accounts/verify_credentials (dev.twitter.com/rest/reference/get/account/verify_credentials)Schmit
@Schmit -- found that in the mean time; just suggested an edit to the answer to clarify for others. Thanks.Instance
(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 call as long as it was made with the include_email option. It won't become available in a users/show response.Instance
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.Parisian
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?Vance
FYI, I found my answer on Twitter Developer Forums (twittercommunity.com/t/…). 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".Vance
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. github.com/DZuz14/CompleteSignInWithTwitterPHPEggert
Hi! After doing the same for email permission to get user email address, still not getting the same. What can be the possible reason?Minny
Twitter updated their API. Now you can configure it in your app permission settings. Follow the next answers.Assimilable
R
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

Rostand answered 17/11, 2016 at 8:18 Comment(6)
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.Eggert
After doing the same. still not getting email address.Minny
not working for me either. any other ways? can i request for email address in development phase?Mew
You will need to regenerate the access token to get the email from an already authenticated user. It's a security from Twitter.Kerstin
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.Cyruscyst
Thanks, @Cyruscyst I just deleted the entry from the table and it worked for meBodoni
C
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

Cortisol answered 13/8, 2015 at 15:33 Comment(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.Bellinzona
E
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

Encourage answered 20/11, 2017 at 18:22 Comment(0)
M
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

Madelene answered 25/3, 2014 at 6:40 Comment(5)
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.Igorot
Thanks Man. I will do as u told.Igorot
Twitter FAQ link is broken :(Hernando
It seems that this might be about to change twittercommunity discussion. It seems its almost implemented but not yet officially documented.Embattled
I think that this is about to change: twittercommunity.com/t/….Embattled
A
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;

}
Anuran answered 19/5, 2016 at 14:24 Comment(1)
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.Dotted

© 2022 - 2024 — McMap. All rights reserved.