簡體   English   中英

JavaScript SDK Facebook:初始化腳本有問題

[英]JavaScript SDK Facebook: Trouble With Init Script

試圖弄清楚如何使用JavaScript SDK。 我是JavaScrip的新手。 我能夠使用PHP SDK,但想在客戶端運行我的東西。 hoo,我不太了解開發站點上的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按鈕以及是否返回訪問令牌等)。

有人可以牽着我的手穿過這開始的部分。 我到處搜索過,但是我不太了解。

編輯:

好的,第二次嘗試身份驗證。 沒用:

<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是oauth所在的位置。

我建議先習慣於調用FB.getLoginStatus() 請參閱: http : //developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/


編輯

根據上面的問題編輯,這應該在window.fbAsyncInit函數中完成

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

並且只有在您檢查了getLoginStatus()之后,您才能知道此人是否已登錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM