开发者

FB.api is not a function

I'm trying to pull all of the posts on a users wall. I got an example running that connects to FB okay, but when I try to use FB.api to get the username (I am definitely successfully connected), I get FB.api is not a function. Some searching found this which suggests that I'm using the wrong API link, but when I try the new one (which seems to connect using the same method) everything firebug NET panel says it loads successfully, there are no errors. . . nothing happens at all.

Here is the code (replace the http://connect.facebook.net/en_US/all.js link with http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php for the code to work up until the error I mentioned):

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head&开发者_StackOverflow中文版gt;
<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>

thanks for any and all help!


You're using methods from two different SDKs. FB.api is part of the new SDK (all.js), and FB.XFBML.Host.parseDomTree() is part of the old SDK (feature loader). For example, to parse the dom tree with the new SDK, you call FB.XFBML.parse(). I recommend you pick one of the two SDKs, preferably the new SDK if you are just starting since the old SDK is deprecated.


I guess you are not intializing correctly. I had similar issues... it was not clear to me which one is oAuth2.0 and which FB-Connect. It seemed confusing. I followed the example (below), it worked for me. See 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>

Hope this helps.


On a side note, you might need extended permission to access publish_stream, read_stream. You should refer here


Typeoneerror is right, you're mixing SDK's.

Here's how I'm initializing the newer JS SDK, using event.subscribe to be sure the SDK is initialized AND the user has authenticated on the JS side before moving forward with any API calls or letting my game try to load etc.

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

I'm also using the PHP SDK to perform the initial auth... If the user is not authed, I echo a JS redirect to our the login url (available from the PHP SDK method getLoginUrl). Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜