简体   繁体   English

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

[英]Facebook user email id using javascript SDK

I am currently using the following code to get the email id of the user using my app 我目前正在使用以下代码来获取使用我的应用程序的用户的电子邮件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);
});

the current code is in a separate javascript file and this function is called when a button is pressed , I have also included <script src="http://connect.facebook.net/en_US/all.js"></script> in the HTML file . 当前代码在一个单独的javascript文件中,当按下按钮时调用此函数,我还包括<script src="http://connect.facebook.net/en_US/all.js"></script>在HTML文件中。 my browser (google chrome) gives me the following error each time I try and access it "Uncaught ReferenceError: FB is not defined " 每次我尝试访问它时,我的浏览器(谷歌浏览器)给我以下错误“未捕获的ReferenceError:FB未定义”

I am relatively new to developing Facebook apps , so all the reference I have is the developers page at facebook , I am sure it is a small error but I just cant wrap my head around it , appreciate it if anybody could point out where I could be going wrong 我对开发Facebook应用程序比较陌生,所以我所拥有的所有参考都是facebook的开发者页面,我确信这是一个小错误,但我无法绕过它,如果有人能指出我可以指出的话,请欣赏它出错了

Your code will not load correctly. 您的代码无法正确加载。 This part: 这部分:

(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));

Should not be inside any function, put this in a script tag, after the body opening. 不应该在任何函数内部,在身体打开后将其放在脚本标记中。 It can't be in a separate file. 它不能在单独的文件中。 This only does a async-load of all.js, that is the Facebook's JS SDK. 这只是all.js的异步加载,即Facebook的JS SDK。 If you are using the async-load, you should not put <script src="http://connect.facebook.net/en_US/all.js"></script> in your document, because this will do a sync-load. 如果您使用的是async-load,则不应在文档中放入<script src="http://connect.facebook.net/en_US/all.js"></script> ,因为这样做会同步 -加载。

This other part of your code: 代码的另一部分:

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

Also should not be inside the function. 也不应该在函数内部。 This fragment is responsible for starting the FB JS SDK in your page, with your app information, it only have to be called one time for each page, otherwise you will be starting the engine multiple times and it will have a caotic behavior. 这个片段负责在你的页面中启动FB JS SDK,并且你的应用程序信息只需要为每个页面调用一次,否则你将多次启动引擎并且它会产生caotic行为。 The "FB" object will only be created after the FB JS SDK loads. 只有在加载FB JS SDK后才会创建“FB”对象。 You don't know when it's going to happen, because of the async load. 由于异步负载,你不知道什么时候会发生。 So you must assign the FB.init to this window event: 因此,您必须将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
    });
};

You want to create a get_id function that uses the Facebook information. 您想要创建一个使用Facebook信息的get_id函数。 You have to be careful with that. 你必须要小心。 With you are sure that this function will be called after the complete load of FB JS SDK, you can create it in any part of your page. 确保在完全加载FB JS SDK后调用此函数,您可以在页面的任何部分创建它。 BUT I think the most safe way is to create it inside the window.fbAsyncInit. 但我认为最安全的方法是在window.fbAsyncInit中创建它。 Don't worry about the token, the SDK does it for you: 不要担心令牌,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'});
    }

};

But when you do that, you will be not sure if your function was already created or not. 但是当你这样做时,你将不确定你的功能是否已经创建。 So when you call it, you will have to test for it's existence: 因此,当你调用它时,你将不得不测试它的存在:

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
}

Good luck! 祝好运!

You need to put: 你需要把:

(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));

out of get_id function. 超出get_id函数。 So it was called before you press the button 因此在按下按钮之前调用它

PS: you don't need to pass ?access_token= manually - FB JS SDK does that for you PS:你不需要传递?access_token=手动 - FB JS SDK为你做了

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

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