使用javascript SDK的Facebook用户电子邮件id

Facebook user email id using javascript SDK

本文关键字:用户 电子邮件 id Facebook javascript SDK 使用      更新时间:2023-09-26

我目前正在使用以下代码来获取使用我的应用的用户的电子邮件id

function get_id(cb){
    FB.init({
        appId  : .......,
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true // parse XFBML
    });
    FB.login(function(response) {
        if (response.authResponse) {
            var token = response.authResponse.accessToken;
            alert(token);
            accessToken = token;
            FB.api('/me?access_token=' + token + '', function (response) {
                cb(response.email);
            });
        }
     },{scope: 'email'});
    (function(d){
         var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement('script'); js.id = id; js.async = true;
         js.src = "//connect.facebook.net/en_US/all.js";
         ref.parentNode.insertBefore(js, ref);
    }(document));

}; // ending of get_id()
get_id(function (email) {
    userID=email;
    alert(email);
});

当前代码在一个单独的javascript文件中,当按下按钮时会调用此函数,我还在HTML文件中包含了<script src="http://connect.facebook.net/en_US/all.js"></script>。我的浏览器(谷歌chrome)每次尝试访问时都会给我以下错误"未捕获引用错误:未定义FB"

我对开发Facebook应用程序相对陌生,所以我所拥有的所有参考资料都是Facebook的开发者页面,我确信这是一个小错误,但我只是无法理解,如果有人能指出我可能在哪里出错,我将不胜感激

您的代码将无法正确加载。本部分:

(function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
}(document));

不应该在任何函数内部,将其放在脚本标记中,在打开正文之后。它不能在单独的文件中。这只会异步加载all.js,也就是Facebook的js SDK。如果使用异步加载,则不应将<script src="http://connect.facebook.net/en_US/all.js"></script>放在文档中,因为这将执行同步加载。

你代码的另一部分:

FB.init({
    appId  : .......,
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true // parse XFBML
});

也不应该在函数内部。这个片段负责在你的页面中启动FB JS SDK,使用你的应用程序信息,每个页面只需要调用一次,否则你将多次启动引擎,它将具有协同行为。只有在加载FB JS SDK之后,才会创建"FB"对象。由于异步负载,您不知道什么时候会发生这种情况。因此,您必须将FB.init分配给此窗口事件:

window.fbAsyncInit = function() {
    FB.init({
        appId  : .......,
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true // parse XFBML
    });
};

您想要创建一个使用Facebook信息的get_id函数。你必须小心。如果你确信这个函数会在FBJS SDK完全加载后被调用,你可以在页面的任何部分创建它。但我认为最安全的方法是在窗口中创建它。fbAsyncInit。不要担心令牌,SDK会为你做这件事:

window.fbAsyncInit = function() {
    FB.init({
        appId  : .......,
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true // parse XFBML
    });
    function get_id(cb){
        FB.login(function(response) {
            if (response.authResponse) {
                FB.api('/me', function (response) {
                    cb(response.email);
                });
            }
        },{scope: 'email'});
    }
};

但当你这样做时,你将不确定你的函数是否已经创建。因此,当你调用它时,你必须测试它的存在:

if(get_id && typeof get_id == 'function') { 
    // It's safe to call your function, go ahead
    get_id(function (email) {
        userID=email;
        alert(email);
    });
} else {
    // Your function does not exists yet, do other stuff
}

祝你好运!

您需要放入:

(function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
}(document));

CCD_ 3功能之外。所以它在你按下按钮之前就被调用了

PS:您不需要手动传递?access_token=-FB JS SDK会为您传递