0

I am using Firebase to allow the user to login to a Unity game via Facebook. It is working fine, but I cannot get access to the user's email by using this parameter auth.CurrentUser.Email. Also, the email is not stored in Firebase Authentication Console. The email can be stored/accessed succussfully when I use other sign-in methods, such as email and google.

Here is my code:

    public void SignInFacebook()
    {
        var perms = new List<string>() { "public_profile", "email", "user_friends" };
        FB.LogInWithReadPermissions(perms, AuthCallback);
    }

    private void AuthCallback(ILoginResult result)
    {
        if (FB.IsLoggedIn)
        {
            // AccessToken class will have session details
            var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
            // Print current access token's User ID
            Debug.Log(aToken.UserId);
            // Print current access token's granted permissions
            foreach (string perm in aToken.Permissions)
            {
                Debug.Log(perm);
            }

            Credential credential = FacebookAuthProvider.GetCredential(aToken.TokenString);
                auth.SignInWithCredentialAsync(credential).ContinueWithOnMainThread(task => {
                    if (task.IsCanceled)
                    {
                        Debug.LogError("SignInWithCredentialAsync was canceled.");
                        return;
                    }
                    if (task.IsFaulted)
                    {
                        Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception);
                        return;
                    }

                    Firebase.Auth.FirebaseUser newUser = task.Result;
                    Debug.LogFormat("User signed in successfully: {0} - {2} - ({1})",
                        newUser.DisplayName, newUser.UserId, newUser.Email);
                });
        }
        else
        {
            Debug.Log("User cancelled login");
        }
    }

And this is what it looks like in the console ("-" is where the email is supposed to be stored. If I use another sign-in method, such as email or google, the email is stored without any issues)

enter image description here

Similar questions were asked about this issue and it was suggested that I change the Account email address setting in Firebase to Prevent creation of multiple accounts with the same email address, but it did not solve the issue.

Thanks!

Mohamed
  • 61
  • 3
  • 9
  • 1
    I found this similar thread as well: https://stackoverflow.com/questions/37522582/empty-email-field-of-firebase-auth-user-using-facebook-login-integration-fireba Have you tried deleting the user and adding them again after changing the Account mail address setting? – Patrick Martin Apr 29 '20 at 15:46
  • @PatrickMartin Yes. It didn't work. – Mohamed Apr 29 '20 at 15:54
  • 1
    @PatrickMartin Actually, at first it didn't work because the account I was using was not verified yet. And even after verification, I got the same issue (didn't have access to the email when signing in). However, I was able to get access to the email when I set the email as the primary contact. So, I guess this is the solution. Thanks! – Mohamed Apr 29 '20 at 16:07

2 Answers2

0

If your Facebook app is in test mode you must login from your Facebook ID. Go to the settings, scroll down, select Apps and Websites, and click on your app. From there, make sure email address require is enabled.

enter image description here

Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
-1

you can try this

private void FacebookAuthCallback(ILoginResult result)
{
    if (FB.IsLoggedIn)
    {
        FB.API("/me?fields=id,name,email", HttpMethod.GET, FacebookGetInfo);
    }
    else
    {
        Debug.Log("User cancelled login");
    }
}

private void FacebookGetInfo(IResult result)
{
    if (result.Error == null)
    {
        if (result.ResultDictionary.ContainsKey("email"))
        {
            string aEmail = result.ResultDictionary["email"].ToString();
            return;
        }
    }
    else
    {
        Debug.Log(result.Error);
    }
}