JavaScript SDK Facebook:初始化脚本的问题

JavaScript SDK Facebook: Trouble With Init Script

本文关键字:脚本 问题 初始化 SDK Facebook JavaScript      更新时间:2023-09-26

试图弄清楚如何使用JavaScript SDK。我是JavaScrip的新手。我可以使用PHP SDK,但想在客户端运行我的东西。总之,我真的不了解开发网站上的SDK文档。所以基本代码是:

<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
    FB.init({
            appId      : 'YOUR_APP_ID', // App ID
            channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
            status     : true, // check login status
            cookie     : true, // enable cookies to allow the server to access the session
            xfbml      : true  // parse XFBML
    });
    // Additional initialization code here
};
// Load the SDK Asynchronously
(function(d){
        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        d.getElementsByTagName('head')[0].appendChild(js);
}(document));

我想我理解。。。它加载SDK,然后在加载后运行FB.init。不过,我不知道把所有的OAuth东西放在哪里,也不知道如何构建登录请求(即,如果没有登录,请单击make a按钮,如果您正在返回访问令牌等)。

有人能牵着我的手走过这段开头吗。我到处找,就是不太明白。

编辑:

好的,第二次尝试身份验证。无效:

<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
    FB.init({
            appId      : 'XXX', // App ID
            channelUrl : 'XXX', // Channel File
            status     : true, // check login status
            cookie     : true, // enable cookies to allow the server to access the session
            xfbml      : true  // parse XFBML
    });
    // Additional initialization code here
    FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
                // the user is logged in and connected to your
                // app, and response.authResponse supplies
                // the user's ID, a valid access token, a signed
                // request, and the time the access token 
                // and signed request each expire
                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;
                FB.api('/me', function(response) {
                        alert(response.name);
                });
            } else if (response.status === 'not_authorized') {
                FB.login(function(response) {
                        if (response.authResponse) {
                            console.log('Welcome!  Fetching your information.... ');
                            FB.api('/me', function(response) {
                                    console.log('Good to see you, ' + response.name + '.');
                                    FB.logout(function(response) {
                                            console.log('Logged out.');
                                    });
                            });
                        } else {
                            console.log('User cancelled login or did not fully authorize.');
                        }
 }, {scope: 'email'});
            } else {
            }
    })
};
// Load the SDK Asynchronously
(function(d){
        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>

谢谢!

就在这条线的下面,// Additional initialization code here这就是愚蠢的东西的去向。

我建议先习惯呼叫FB.getLoginStatus()。请参阅:http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/


编辑

基于以上问题编辑这应该在窗口内完成。fbSyncInit函数

FB.api('/me', function(response) {
        alert(response.name);
});

并且只有在检查了getLoginStats()之后,才能知道此人是否登录。