2

試圖弄清楚如何使用JavaScript SDK。我是JavaScrip的新手。我能夠使用PHP SDK,但想運行我的東西clientside。 Anyhoo,我不太瞭解開發網站上的SDK文檔。因此,基本代碼是:JavaScript SDK Facebook:使用Init Script的問題

<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東西,以及如何構建登錄請求(即如果沒有登錄製作一個按鈕,如果你是然後返回訪問令牌等

有人可以把我的。一方面通過這個開頭部分我搜索無處不在,我只是不太明白

編輯:

好,有認證的第二次嘗試沒有工作。

<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> 

Thanks!

回答

1

就在這條線下面, // Additional initialization code here 是oauth的東西去的地方。

我建議習慣先撥打FB.getLoginStatus()。請參閱:http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/


編輯

基於上述 問題編輯這應該在window.fbAsyncInit函數內部進行檢查完getLoginStatus

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

,僅在()讓你知道這個人是否登錄。

+0

請參閱編輯。感謝您的幫助btw :) – JoshDG 2012-02-02 16:03:28

+0

編輯agin。仍然沒有顯示出來。控制檯中沒有任何一個.. – JoshDG 2012-02-02 16:59:50

+1

堅持,現在我看到一個語法錯誤。 – JoshDG 2012-02-02 17:03:14