Facebook SDK:有时电子邮件字段返回null(设置了"email"权限)

Rap*_*tor 0 facebook

我有一个用PHP编写的Facebook应用程序.询问用户的"电子邮件"权限.我注意到我获得了90%的用户电子邮件地址,但不是一些.

我知道电子邮件权限允许Facebook App获取用户个人资料中声明的主要电子邮件地址,但为什么SDK有时会返回null

Fis*_*sch 5

"email"是一种扩展权限,用户在申请时不需要提供.如果您需要用户在继续使用应用程序之前授予"电子邮件"权限,则需要在允许用户在身份验证后继续其工作流程之前检查该权限是否存在.

您需要使用的图形API端点是 "/me/permissions"

这是我用来做的一种方法:

permissions 是一个表示请求权限的字符串数组.

hasPermissions(["email"], function(hasPerms){
     alert(hasPerms);
 });

function hasPermissions(permissions, callback){
    console.log("perms requested");
    console.log(permissions);
    FB.api("/me/permissions", function(response){
        var hasPerms = true;
        console.log("perms obtaind");
        console.log(response);
        for(var i in permissions){
            console.log([permissions[i]] + " - " + response["data"][0][permissions[i]])
            hasPerms = hasPerms && response["data"][0][permissions[i]] == 1;
        }
        if(typeof callback == "function"){
            callback(hasPerms);     
        }       
    });
}
Run Code Online (Sandbox Code Playgroud)