简体   繁体   English

在我的网站上登录Facebook-javascript sdk

[英]login to facebook in my website - javascript sdk

I am working in the task of login to facebook from my website, i got the code from here github . 我正在从我的网站登录Facebook的任务,我从这里github获得了代码。 here is the code: 这是代码:

    $(function() {

    var app_id = my_app_id;
    var scopes = 'email, user_friends, user_online_presence';

    var btn_login = '<a href="#" id="login" class="btn btn-primary">Log In</a>';

    var div_session = "<div id='facebook-session'>"+
                      "<strong></strong>"+
                      "<img>"+
                      "<a href='#' id='logout' class='btn btn-danger'>Log Out</a>"+
                      "</div>";

    window.fbAsyncInit = function() {
            alert("init");

        FB.init({
            appId      : app_id,
            status     : true,
            cookie     : true, 
            xfbml      : true, 
            version    : 'v2.1'
        });


        FB.getLoginStatus(function(response) {
            statusChangeCallback(response, function() {});
        });
    };

    var statusChangeCallback = function(response, callback) {
        console.log(response);

        if (response.status === 'connected') {
            getFacebookData();
        } else {
            callback(false);
        }
    }

    var checkLoginState = function(callback) {
        FB.getLoginStatus(function(response) {
            callback(response);
        });
    }

    var getFacebookData =  function() {
        FB.api('/me', function(response) {
            $('#login').after(div_session);
            $('#login').remove();
            $('#facebook-session strong').text("welcome: "+response.name);
            $('#facebook-session img').attr('src','http://graph.facebook.com/'+response.id+'/picture?type=large');
        });
    }

    var facebookLogin = function() {
        checkLoginState(function(data) {
            if (data.status !== 'connected') {
                FB.login(function(response) {
                    if (response.status === 'connected'){
                        getFacebookData();
                    }
                }, {scope: scopes});
            }
        })
    }

    var facebookLogout = function() {
        checkLoginState(function(data) {
            if (data.status === 'connected') {
                FB.logout(function(response) {
                    $('#facebook-session').before(btn_login);
                    $('#facebook-session').remove();
                })
            }
        })

    }



    $(document).on('click', '#login', function(e) {
        e.preventDefault();

        facebookLogin();
    })

    $(document).on('click', '#logout', function(e) {
        e.preventDefault();

        if (confirm("Are you sure"))
            facebookLogout();
        else 
            return false;
    })

})

I made an account on facebook developers and got the id which i put in the code (my_app_id), the problem is that when i login with another account, the response object returned gives me "not_authorized" status. 我在facebook开发人员上创建了一个帐户,并获得了我输入代码(my_app_id)中的ID,问题是当我使用另一个帐户登录时,返回的响应对象给了我“ not_authorized”状态。 I want to be able to login with any account and get the info i want (like user_name, profile_picture, etc...). 我希望能够使用任何帐户登录并获取我想要的信息(例如user_name,profile_picture等)。 I got these information only when i login with the account that i created on the facebook developers. 仅当我使用在Facebook开发人员上创建的帐户登录时,我才获得这些信息。 Another problem is here: 另一个问题在这里:

var facebookLogin = function() {
    checkLoginState(function(data) {
        if (data.status !== 'connected') {alert("facebookLogin");
            FB.login(function(response) {
                if (response.status === 'connected'){
                    getFacebookData();
                }
            }, {scope: scopes});
        }
    })
}

it gets the facebook data when i am logged in the account, where is the condition where the account is not logged in. I mean there is a line checks if the user is logged in if (data.status !== 'connected') , where is the else condition the verify that the user is not logged in? 当我登录该帐户时,它会获取facebook数据,这是该帐户未登录的条件。我的意思是, if (data.status !== 'connected')该行将检查用户是否已登录,在其他情况下验证用户未登录?

I recommend you start your testing by using the latest APIs and guides in our docs in a blank page/project without jQuery or other third-party libraries. 我建议您在没有jQuery或其他第三方库的空白页/项目中,使用我们文档中的最新API和指南开始测试。

Once everything is setup and working, start adding other libraries and see how it goes. 一切设置完成并开始工作后,开始添加其他库并查看其运行情况。

Moreover, if you are having issues testing your app with different accounts, it's most likely because your app is on Development Mode and the account you are testing with is not added in the app's role and hence they cannot actually access your app. 此外,如果您在使用其他帐户测试应用程序时遇到问题,则最有可能是因为您的应用程序处于开发模式,并且您所使用的测试帐户未添加到该应用程序的角色中,因此他们实际上无法访问您的应用程序。 Try add the other accounts as admin , developer or tester and test again: https://developers.facebook.com/apps/{APP-ID}/roles/ 尝试将其他帐户添加为admindevelopertester然后再次进行测试: https://developers.facebook.com/apps/{APP-ID}/roles/ : https://developers.facebook.com/apps/{APP-ID}/roles/

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

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