Facebook oauth检索用户电子邮件

Ste*_*fin 38 facebook facebook-graph-api

我使用Facebook Javascript SDK获取oauth令牌并从FB检索所有数据:

 function login() {
    FB.api('/me', function (response) {            
        alert('You have successfully logged in, ' + response.name + ", " + response.email + "!");

    });
Run Code Online (Sandbox Code Playgroud)

我检索所有基本信息(全名,工作,学校,ID ..)但不是EMAIL(response.email == undefined)!

我知道可以在基本信息中访问电子邮件(不要求'电子邮件'权限),我该怎么做?

Bal*_*nan 69

您需要明确地收到如下电子邮件:

                    var url = '/me?fields=name,email';
                    FB.api(url, function (response) {
                        alert(response.name);
                        alert(response.email);
                    });
Run Code Online (Sandbox Code Playgroud)

要获取电子邮件地址,需要电子邮件权限.所以代码可能看起来像(还有其他选项,如注册按钮,登录按钮..)

            FB.login(function (response) {
                if (response.session) {
                    var url = '/me?fields=name,email';
                    FB.api(url, function (response) {
                        alert(response.name);
                        alert(response.email);
                    });
                }
                else {
                    alert("User did not login successfully");
                }
            }, { scope: 'email' }); /* perms changed to scope */
Run Code Online (Sandbox Code Playgroud)


Vih*_*ith 6

首先让您的应用现场,提供给公众。然后,您必须授予权限以获取用户“电子邮件”,这是应用程序默认不允许的。之后,应按以下方式更改登录方法。

function checkLoginState() {
    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
        console.log('Welcome!  Fetching your information.... ');
        var url = '/me?fields=id,name,email';
        FB.api(url, function(response) {
             alert(response.name + " " + response.id + " " +response.email);
        }, {scope: 'email'});
    });
}
Run Code Online (Sandbox Code Playgroud)

如果不需要字段ID,则将变量url更改 为:

var url = '/me?fields=name,email';
Run Code Online (Sandbox Code Playgroud)