Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Facebook Unity SDK - Get user's email address

Discussion in 'Scripting' started by matbrummitt, Oct 14, 2015.

  1. matbrummitt

    matbrummitt

    Joined:
    Jan 12, 2010
    Posts:
    107
    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.

    Code (CSharp):
    1.     void HandleOnClickFacebookLoginButton (System.EventArgs e) {
    2.         if (FB.IsInitialized)
    3.             FB.LogInWithReadPermissions (new List<string>(){"public_profile", "email"}, AuthCallback);
    4.     }
    5.  
    6.     void AuthCallback (ILoginResult result) {
    7.  
    8.         if (!FB.IsLoggedIn) {
    9.             Debug.Log ("User cancelled login");
    10.             return;
    11.         }
    12.  
    13.         FetchFBProfile ();
    14.  
    15.         if (OnLoggedIn != null)
    16.             OnLoggedIn (new OnLoggedInEventArgs ());
    17.     }
    18.  
    19.     private void FetchFBProfile () {
    20.         FB.API("/me?fields=first_name,last_name,email", HttpMethod.GET, FetchProfileCallback, new Dictionary<string,string>(){});
    21.     }
    22.  
    23.     private void FetchProfileCallback (IGraphResult result) {
    24.  
    25.         Debug.Log (result.RawResult);
    26.  
    27.         FBUserDetails = (Dictionary<string,object>)result.ResultDictionary;
    28.         StartCoroutine(FetchFBProfilePicture ());
    29.  
    30.         Debug.Log ("Profile: first name: " + FBUserDetails["first_name"]);
    31.         Debug.Log ("Profile: last name: " + FBUserDetails["last_name"]);
    32.         Debug.Log ("Profile: id: " + FBUserDetails["id"]);
    33.         Debug.Log ("Profile: email: " + FBUserDetails["email"]);
    34.  
    35.     }
    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:

    Code (CSharp):
    1.     void AuthCallback (ILoginResult result) {
    2.  
    3.         foreach (string perm in Facebook.Unity.AccessToken.CurrentAccessToken.Permissions)
    4.             Debug.Log(perm);
    5.     }
    However, a debug.log for the user ID returns "mockUserId" rather than an actual ID:

    Code (CSharp):
    1. Debug.Log(Facebook.Unity.AccessToken.CurrentAccessToken.UserId)
    2.  
    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
     
    Last edited: Oct 14, 2015
  2. matbrummitt

    matbrummitt

    Joined:
    Jan 12, 2010
    Posts:
    107
    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:
    https://developers.facebook.com/tools/explorer

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

    Thanks

    Mat
     
  3. BackwoodsGaming

    BackwoodsGaming

    Joined:
    Jan 2, 2014
    Posts:
    2,229
    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?
     
  4. matbrummitt

    matbrummitt

    Joined:
    Jan 12, 2010
    Posts:
    107
    Hi Shawn,

    This project is my first outing with Facebook. I am using the SDK available here (version 7.2.0):
    https://developers.facebook.com/docs/unity/downloads

    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
     
  5. BackwoodsGaming

    BackwoodsGaming

    Joined:
    Jan 2, 2014
    Posts:
    2,229
    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! :)
     
  6. A_munim

    A_munim

    Joined:
    Mar 11, 2017
    Posts:
    5
  7. unity_yJhvSbiWiR6Vdg

    unity_yJhvSbiWiR6Vdg

    Joined:
    Jun 22, 2018
    Posts:
    4
     
  8. unity_yJhvSbiWiR6Vdg

    unity_yJhvSbiWiR6Vdg

    Joined:
    Jun 22, 2018
    Posts:
    4
    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