2010-12-22 212 views
0

我試圖拉用戶牆上的所有帖子。我有一個連接到FB的示例運行沒問題,但是當我嘗試使用FB.api獲取用戶名(我肯定已成功連接)時,我得到FB.api is not a function。一些搜索發現this這表明我使用了錯誤的API鏈接,但是當我嘗試new one(似乎使用相同的方法連接)時,一切螢火蟲的NET面板表示它加載成功,沒有錯誤。 。 。什麼都沒有發生。FB.api不是函數

下面是代碼(替換http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.phphttp://connect.facebook.net/en_US/all.js鏈接的代碼工作,直到我提到的錯誤):

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml"> 
<head> 
<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script> 
<script type="text/javascript"> 
function grab(){ 
FB.api('/me', function(response) { 
    alert(response.name); 
    }); 
} 
function update_user_box() { 
var user_box = document.getElementById("user"); 
user_box.innerHTML = 
"<div>" 
+ "<fb:profile-pic uid='loggedinuser' facebook-logo='true'></fb:profile-pic>" 
+ " Welcome, <fb:name uid='loggedinuser' useyou='false'></fb:name>" 
+ "</div>" 
+ "<a href=\"javascript:grab()\">try me</a>"; 
FB.XFBML.Host.parseDomTree(); 
} 
</script> 
</head> 
<body> 


<div id='user'><fb:login-button onlogin="update_user_box();"></fb:login-button></div> 
<br><br> 

<div id="fb-root"></div> 
<script type="text/javascript"> 
FB.init("b07e858317c9069d450023b7500b4511", "xd_receiver.htm", {"ifUserConnected": update_user_box}); 

</script> 
</body> 
</html> 

感謝任何及所有幫助!

回答

1

我想你不是正確的初始化。我有類似的問題......我不清楚哪一個是oAuth2.0和哪個FB-Connect。這似乎令人困惑。我遵循下面的例子,它爲我工作。見here

<div id="fb-root"></div> 
<script src="http://connect.facebook.net/en_US/all.js"></script> 
<script> 
    FB.init({ 
    appId : 'YOUR APP ID', 
    status : true, // check login status 
    cookie : true, // enable cookies to allow the server to access the session 
    xfbml : true // parse XFBML 
    }); 
</script> 

希望這會有所幫助。


在一個側面說明,你可能需要延長的訪問權限publish_stream,read_stream。您應該參考here

3

您正在使用來自兩個不同SDK的方法。 FB.apinew SDK(all.js)的一部分,FB.XFBML.Host.parseDomTree()old SDK(功能加載程序)的一部分。例如,要使用新SDK解析dom樹,請致電FB.XFBML.parse()。我建議你選擇兩個SDK中的一個,最好是新SDK,如果你剛從舊SDK推出時開始使用。

0

Typeoneerror是對的,你在混合SDK的。

下面是我如何初始化較新的JS SDK,使用event.subscribe來確保SDK已初始化並且用戶已經在JS端進行了身份驗證,然後再向前移動任何API調用或讓我的遊戲嘗試加載等。

<div id="fb-root"> </div> 
<script> 
    window.fbAsyncInit = function() { 
    FB.init({appId: 'YOUR_APP_ID', status: true, cookie: true, xfbml: true}); 
    FB.Canvas.setSize({ width: 760, height: 1000 }); // use this to increase canvas height 
    FB.Event.subscribe('auth.login', function(response) { 
    // You can call your function that requires FB.whatever here... 
    }); 
}; 
(function() { 
var e = document.createElement('script'); e.async = true; 
e.src = document.location.protocol + 
    '//connect.facebook.net/en_US/all.js'; 
document.getElementById('fb-root').appendChild(e); 
}()); 
</script> 

我還使用了PHP SDK執行初始身份驗證...如果用戶沒有authed,我贊同一個JS重定向到我們的登錄URL(可從PHP SDK方法getLoginUrl) 。希望有所幫助。