Facebook Unity SDK - Get user's email address

Hi all,

I am implementing Facebook logins into my game and I would like the user’s email address. I realise that I need to request “extended permissions” to access the email address. I have done this. However, when I use FB.API() to retrieve the user’s information, everything comes back except for the email address. I am attempting this in the Unity editor, where i’m entering the access token given to me by Facebook.

    void HandleOnClickFacebookLoginButton (System.EventArgs e) {
        if (FB.IsInitialized)
            FB.LogInWithReadPermissions (new List<string>(){"public_profile", "email"}, AuthCallback);
    }

    void AuthCallback (ILoginResult result) {

        if (!FB.IsLoggedIn) {
            Debug.Log ("User cancelled login");
            return;
        }

        FetchFBProfile ();

        if (OnLoggedIn != null)
            OnLoggedIn (new OnLoggedInEventArgs ());
    }

    private void FetchFBProfile () {
        FB.API("/me?fields=first_name,last_name,email", HttpMethod.GET, FetchProfileCallback, new Dictionary<string,string>(){});
    }

    private void FetchProfileCallback (IGraphResult result) {

        Debug.Log (result.RawResult);

        FBUserDetails = (Dictionary<string,object>)result.ResultDictionary;
        StartCoroutine(FetchFBProfilePicture ());

        Debug.Log ("Profile: first name: " + FBUserDetails["first_name"]);
        Debug.Log ("Profile: last name: " + FBUserDetails["last_name"]);
        Debug.Log ("Profile: id: " + FBUserDetails["id"]);
        Debug.Log ("Profile: email: " + FBUserDetails["email"]);

    }

Debug.Log (result.RawResult) shows that the email field is not included at all - it’s not just null, it’s actually not there at all.

UPDATE:

I have run my access token through the access token debugger. It shows the token has the following scopes: user_friends, public_profile. This is obviously why the email is not available, but since my code requests email permissions, I don’t know why the access token fails to have this scope.

UPDATE 2:

I am starting to think that the cause of this is a bug with the latest Facebook Unity SDK. The following code loops through the available permissions, and “email” is included:

    void AuthCallback (ILoginResult result) {

        foreach (string perm in Facebook.Unity.AccessToken.CurrentAccessToken.Permissions)
            Debug.Log(perm);
    }

However, a debug.log for the user ID returns “mockUserId” rather than an actual ID:

Debug.Log(Facebook.Unity.AccessToken.CurrentAccessToken.UserId)

I believe when using FB.API(), it’s this accessToken that is used. Since the User’s ID is not set, perhaps this in turn affects the request for the email address.

Googling “Facebook Unity mockUserId” returns a recent result from Twitter, where one developer asks why Facebook.Unity.AccessToken.CurrentAccessToken.UserId is hard-coded to “mockUserId”. See tweet

UPDATE 3:

Following my thoughts on Update 2, I tried updating the class to hard-code my User ID rather than “mockUserId” but this had no effect, and the issue is still there.

Any thoughts?

Any help is greatly appreciated.

Thanks

Mat

FIXED:

A new version of the Facebook Unity SDK was released and with it, some additional information in the documentation (unless I just missed it before).

It states that in the Unity Editor, the permissions granted may not include everything that was requested with FB.LogInWithReadPermissions(). For testing, you need to use the Graph Explorer to generate an access token that will include every permission that you specify. You simply copy and paste this into the modal that shows when running the game and triggering a Facebook login.

See Graph Explorer:

This works, the email address is returned. Headache over.

Thanks

Mat

Hey Mat! I haven’t started with any Facebook integrations yet but have been considering it for an upcoming project. Out of curiosity, which SDK are you using? In the asset store I’m seeing the free one from Unity as well as a couple of others. Would love to get some feedback on one that is working well. Other than this issue that you found a workaround for, have you had any other issues with the one you are using?

Hi Shawn,

This project is my first outing with Facebook. I am using the SDK available here (version 7.2.0):

The official SDK from Facebook doesn’t seem to be all that bad - the documentation isn’t the best, but with some experimenting and looking over the example projects, it’s reasonably straight forward to use. The recently updated documentation has better examples that are easier to follow, so I would recommend giving this a go first before buying a 3rd party package. Keep in mind that the official Facebook SDK does not support the web player.

As for other issues, well, i’ve had a few problems that weren’t covered by the docs. Some of these are fixed in the later SDK’s anyway.

I am considering writing a short video series on implementing Facebook into a Unity project because I found good resources were hard to come by, and I think everything i’ve learned would be useful to others if presented over a good quality set of videos. Would you be interested in that?

Thanks

Mat

Thanks for the info, Mat… Bookmarked the link. No worries on the lack of web player support. Especially since Unity has announced end of life for it. Sounds like WebGL is going to be it’s replacement, at least that is the understanding I got from quick glance of the blog post.

I think a short tutorial series would be an awesome addition. I haven’t started looking as I’m just now to the point of putting thought into some small mobile apps or possibly Facebook specific apps. I’ve barely started researching it at this point. But if, like you said, resources are scarce then I think someone putting together a good series would rock! :slight_smile:

Wow @matbrummitt you just saved my Day!

[quote=mat
brummitt, post: 2338841, member: 14427]
nity.AccessToken.CurrentAccessToken.UserI
[/quote]

can you explain it to me in more detailed way i already get username userid and profile i need to get email and friends id can you help me