简体   繁体   中英

Unity : Get Facebook email by Facebook SDK

I'm using facebook SDK for my app login, and try to access id, name and email. I follow the reference from facebook developer FB.LogInWithReadPermissions

public void FBLogin(){
    FB.LogInWithReadPermissions (new List<string>(){"public_profile","email","user_friends"}, 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

        // Print current access token's granted permissions
        foreach (string perm in aToken.Permissions) {
            Debug.Log(perm);
        }
        FB.API ("/me?fields=id,name,email",HttpMethod.GET, GetFacebookInfo);
    } else {
        Debug.Log("User cancelled login");
    }
}

public void GetFacebookInfo(IResult result){
    if (result.Error == null) {
        Debug.Log (result.ResultDictionary ["id"].ToString ());
        Debug.Log (result.ResultDictionary ["name"].ToString ());
        Debug.Log (result.ResultDictionary ["email"].ToString ());
    } else {
        Debug.Log (result.Error);
    }
}

Error I get in Debug.Log (result.ResultDictionary ["email"].ToString ());

KeyNotFoundException: The given key was not present in the dictionary.

Did I miss something ? How can I able to get the email. Thanks a lot.

Try to change the line

    FB.API ("/me?fields=id,name,email",HttpMethod.GET, GetFacebookInfo);

into

        FB.API ("/me?fields=id,name,email", HttpMethod.GET, GetFacebookInfo, new Dictionary<string, string> () { });

I remember having the same error and it worked for me.

If you are using a test user, then you must add the 'email' permission to the test user.

Go to 'facebook for developers' and select your app ('Wizard's Run' for me):

在此处输入图片说明

Then go to 'Test Users'

在此处输入图片说明

Then go to the user that you want to test with and select 'Change permissions this test user granted to app'

在此处输入图片说明

If 'email' isn't already listed, then enter 'email' and Update.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM