简体   繁体   English

Unity:通过Facebook SDK获取Facebook电子邮件

[英]Unity : Get Facebook email by Facebook SDK

I'm using facebook SDK for my app login, and try to access id, name and email. 我使用Facebook SDK进行我的应用登录,并尝试访问ID,名称和电子邮件。 I follow the reference from facebook developer FB.LogInWithReadPermissions 我遵循来自Facebook开发人员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 ()); 我在Debug.Log (result.ResultDictionary ["email"].ToString ());得到错误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): 转到“开发人员facebook”并选择您的应用(“ Wizard's Run”对我而言):

在此处输入图片说明

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. 如果尚未列出“电子邮件”,则输入“电子邮件”并更新。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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