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