How do I get a persons email?

Nov 11, 2010 at 10:30 AM

Hi everyone,

I've got login working fine. I just need to get the users email address so a can match existing users in my site.

 

So far, api.Users.GetInfo().email_hashes has not helped me much and google has absolutly nothing about this.

 

I did read somewhere that email can be retrieved using Graph API, is that true? If so, why can't a I do it with this API?

 

Thanks a bunch.

Nov 12, 2010 at 2:49 PM

It is not that hard.

response = api.Fql.Query(String.Format("SELECT first_name, last_name, email, current_location from user where uid = {0}", user.UserID));

Normal guidance regarding having a current session or offline_access extended permission, and email extended permissions apply

Nov 12, 2010 at 10:14 PM

For me it is hard

That query simply returns this

 

<?xml version="1.0" encoding="UTF-8"?>

<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">  <user>    <first_name>Vince</first_name>    <last_name>Pana</last_name>    <email xsi:nil="true"/>    <current_location xsi:nil="true"/>  </user></fql_query_response>

 

What am I missing?

 

Nov 13, 2010 at 2:38 PM

You may be missing the email extended permission, and you may also be missing a current session for the user in question.

I've always found prompting for extended permissions using this toolkit to be a sub-optimal user experience (I prefer to only prompt for the permissions I need right when I need them rather than all up front on page load, I think users respond better to permissions requests when it is a result of them turning on a feature by clicking on a checkbox), so I do that bit in the javascript client library:

function EmailPerms() {
    FB.login(function(response) {
        if (response.session) {
            if (response.perms) {
                $('#SessionKey').val(response.session.session_key);
                $('#SessionSecret').val(response.session.secret);
            } else {
                // permissions not granted...reprompt
            }
        } else {
            // user is not logged in
        }
    }, { perms: 'email' });
}

Then I pass the session_key and secret back to the server where I use them for the fql query (set api.Session.SessionKey before making the fql query)  Of course you could always do the fql in javascript as well, whatever floats your boat.

If you are querying for the email address without having a current session with the user (session is expired), you will also need the offline_access extended permission

Nov 13, 2010 at 10:38 PM

That still doesn't work. Here's the error message I get.

Microsoft JScript runtime error: Object doesn't support this property or method

Here is my code . I've also tried loading <script src="http://connect.facebook.net/en_US/all.js"></script> but then I get object "root" is undefined error.

<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>

function EmailPerms() {
    FB.login(function(response) {
        if (response.session) {
            if (response.perms) {
                $('#SessionKey').val(response.session.session_key);
                $('#SessionSecret').val(response.session.secret);
            } else {
                // permissions not granted...reprompt
            }
        } else {
            // user is not logged in
        }
    }, { perms: 'email' });
}

....

</body>

<script>FB.init('<% = ConfigurationManager.AppSettings["APIKey"] %>', "/xd_Receiver.htm");</script
>

Nov 14, 2010 at 4:53 PM

I think you are try to use the deprecated javascript client library.  Use the new one:  https://github.com/facebook/connect-js

Nov 14, 2010 at 5:01 PM

Also since you seem kinda new at Facebook development, I just want to be sure you realize that this particular .net client library for Facebook is not really being actively maintained by its developers.  It is probably not a good choice if you are just getting started or building a new app.  I just check in here once in a while because I have an app that still runs on it.  It does work quite well for my purposes, but at some point I am sure I am going to have to abandon this library and move on to one of the new ones.  Facebook makes somewhat radical changes in their api about once a year or so that have the potential to break apps using this library if the library doesn't change to reflect the changes Facebook makes.

If I was going to write an app from scratch today I would look at http://facebooksdk.codeplex.com/

Nov 14, 2010 at 9:43 PM

Thanks for posting that info.

I think I'll switch to that new API you suggeted.

I don't understand, this library had a release April this year so it's not that old... Never mind. Thanks for your time anyway.