2010-01-21 23 views
0

從這個非常好的article about doing facebook integration using the javascript API開始,我能夠成功地對FB.Connect命名空間進行多次調用。無法弄清楚如何在js庫中調用Facebook ApiClient

我現在對FB.ApiClient庫中的調用感興趣,並嘗試了一些簡單的調用。我無法得到這個工作,有什麼我必須做的不同? 具體來說,我想調用events_get方法。

文檔:

代碼:

工作的JavaScript:

function updateStatus() { 
     FB.Connect.streamPublish(); 
    } 

錯誤:FB.ApiClient.get_apiKey不

function getEventInfo() { 
     FB.ApiClient.get_apiKey(); 
    } 

回答

1

命名空間和方法在命名和使用方式上是不一致的。要訪問apiClient,您必須使用FB.Facebook.apiClient。

總之。

FB.apiClient.get_apiKey(); 

電話:而不是調用的

FB.Facebook.apiClient.get_apiKey(); 

下面,我已經包含了一個完整的例子,我在那裏工作了的Connect和ApiClient API的幾種方法。這是截至2009年1月22日。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml"> 
<body> 
<a target="_blank" href="http://developers.facebook.com/docs/?u=facebook.jslib">Facebook JavaScript API Documentation</a> 
<div id="comments_post"> 
    <h3>Post a comment to your Facebook Feed:</h3> 

    Demonstration to show what can be done with Facebook Connect, using the javascript API 

    <form name="comment_form" method="POST"> 
     <div id="user"> 
      Name: <input name="name" size="27"><br /> 
      <fb:login-button length='long' onlogin="update_user_box();"></fb:login-button> 
     </div> 

     <br /><textarea name="comment" rows="5" cols="30"></textarea> 

     <br /><input type="button" onclick="submit_comment();" value="Submit Comment"> 
     <br /><input type="button" onclick="updateStatus();" value="updateStatus"> 
     <br /><input type="button" onclick="updateStatusWithImage();" value="updateStatusWithImage"> 
     <br /><input type="button" onclick="postToFriendsWall();" value="postToFriendsWall"> 
     <br /><input type="button" onclick="postAnImage();" value="postAnImage"> 
     <br /><input type="button" onclick="alert(FB.Connect.get_loggedInUser());" value="get logged in user id"> 
     <br /><input type="button" onclick="getFriends()" value="get friends"> 
     <br /><input type="button" onclick="getEventMembers()" value="get event members"> 
     <br /><input type="button" onclick="getEventInfo()" value="get event info"> 

     <div id="profile_pics"></div> 

    </form> 
</div> 

<script type="text/javascript"> 
    function update_user_box() { 
     var user_box = document.getElementById("user"); 

     user_box.innerHTML = 
      "<span>" 
      + "<fb:profile-pic uid='loggedinuser' facebook-logo='true'></fb:profile-pic>" 
      + "Welcome, <fb:name uid='loggedinuser' useyou='false'></fb:name>" 
      + "</span>"; 

     FB.XFBML.Host.parseDomTree(); 
    } 

    function update_user_not() { 
    } 

    function submit_comment() { 

     comment_text = document.getElementsByName("comment")[0].value; 

     var template_var = { "post-title": "pakt.com", 
      "post-url": "http://www.pakt.com/pakt/?id=ce00f49ed79e17aa&t=How_to_add_Facebook_Connect_to_your_website", 
      "comment-text": comment_text, 
      "images": [{ "src": "http://images.pakt.com/images/user/chris/thumb_chris_5df0eb914796c68.jpg", "href": "http://www.pakt.com/pakt/?id=ce00f49ed79e17aa&t=How_to_add_Facebook_Connect_to_your_website"}] 
     }; 

     FB.Connect.streamPublish(comment_text); 
    } 

    function ClearComments() { 
     document.getElementsByName("comment")[0].value = ""; 
    } 

    function updateStatus() { 
     FB.Connect.streamPublish(); 
    } 
    var attachment = { 'media': [{ 'type': 'image', 
     'src': 'http://bit.ly/AJTnf', 
     'href': 'http://bit.ly/hifZk'}] 
    }; 
    function updateStatusWithImage() { 

     FB.Connect.streamPublish('', attachment); 
    } 

    function postToFriendsWall() { 


     FB.Connect.streamPublish('enjoying the Facebook javascript API much better than other versions', attachment, null, 1578234238); 
    } 

    function postAnImage() { 
     function stream_callback(post_id, exception) { 
      if (post_id) { 
       post_to_my_server(post_id); 
      } 
     } 

     FB.Connect.streamPublish('', attachment, null, null, 
        'What do you think?', 
        stream_callback); 
    } 

    function getLoggedInUser() { 
     var id = FB.Connect.get_loggedInUser(); 
     alert(id); 
    } 

    function getFriends() { 
     var widget_div = document.getElementById("profile_pics"); 
     FB.ensureInit(function() { 

      FB.Facebook.apiClient.friends_get(null, function(result) { 
       var markup = ""; 
       var num_friends = result ? Math.min(10, result.length) : 0; 
       if (num_friends > 0) { 
        for (var i = 0; i < num_friends; i++) { 
         markup += 
        '<fb:profile-pic size="square" uid="' 
        + result[i] 
        + '" facebook-logo="true">' 
        + ' </fb:profile-pic>'; 
        } 
       } 
       widget_div.innerHTML = markup; 
       FB.XFBML.Host.parseDomElement(widget_div); 
      }); //end of friends_get 
     }); //end of ensureInit 
    } 

    function getEventMembers() { 
     var widget_div = document.getElementById("profile_pics"); 
     FB.ensureInit(function() { 

      FB.Facebook.apiClient.events_getMembers(53288226963, function(result) { 
       var markup = ""; 
       var num_friends = result ? Math.min(10, result.attending.length) : 0; 
       if (num_friends > 0) { 
        for (var i = 0; i < num_friends; i++) { 
         markup += 
        '<fb:profile-pic size="square" uid="' 
        + result.attending[i] 
        + '" facebook-logo="true">' 
        + ' </fb:profile-pic>'; 
        } 
       } 
       widget_div.innerHTML = markup; 
       FB.XFBML.Host.parseDomElement(widget_div); 
      }); //end of friends_get 
     }); //end of ensureInit 
    } 

    //this one doesn't work. don't know why yet. 
    function getEventInfo() { 
     var widget_div = document.getElementById("profile_pics"); 
     var eids = new Array(); 
     eids.push(53288226963); 

     FB.Facebook.apiClient.events_get('', eids, 0, 0, 'attending', function(result) { 
      var markup = ""; 
      var num_friends = result ? Math.min(10, result.attending.length) : 0; 
      if (num_friends > 0) { 
       for (var i = 0; i < num_friends; i++) { 
        markup += 
        '<fb:profile-pic size="square" uid="' 
        + result.attending[i] 
        + '" facebook-logo="true">' 
        + ' </fb:profile-pic>'; 
       } 
      } 
      widget_div.innerHTML = markup; 
      FB.XFBML.Host.parseDomElement(widget_div); 
     }); //end of friends_get 

    } 
</script> 
<script type="text/javascript" src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" mce_src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"> </script> 
<script type="text/javascript"> 
    var api_key = "de57fdca3c665931e2a6b411f90a1cb4"; 
    var channel_path = "xd_receiver.htm"; 
    FB.init(api_key, channel_path, { "ifUserConnected": update_user_box }); 
</script> 
</body> 
</html> 
0

FB.ApiClient定義的函數?你有沒有試過用FB_RequireFeatures調用包裝它,並確保它已初始化?例如。

FB_RequireFeatures(["Api"], function(){ 
      FB.Facebook.init('b9d3cab9951682757d0488290545d854', 'xd_receiver.htm'); 
      FB.Facebook.get_sessionState().waitUntilReady(function(){ 
       FB.Facebook.apiClient.friends_get(null, function(result, ex) { 
        alert(result) 
       }); 
     }); 
+0

它似乎被定義。我將ApiClient的大小寫更改爲apiClient,並且新錯誤是FB.apiClient未定義。我嘗試使用上面的代碼,但我得到了同樣的錯誤。替代我的api_key'api_key',爲我自己的道路尋找路徑。 – MedicineMan 2010-01-21 19:23:12

+0

我編輯了我的答案,我已經設法爲friends_get api調用工作 - 所以其他任何人都應遵循。 – 2010-01-22 19:02:36

+0

感謝您發佈該信息。任何想法爲什麼你調用FB.Connect.streamPublish vs FB.Facebook.apiClient.friends_get?這真是令人困惑,相當令人沮喪。我看到你在爲SocialCash工作。我看到SocialCash正在維護Facebook的.NET包裝。與以下版本相比,它們的.NET包裝器如何:www.codeplex.com/FacebookToolkit?我在使用codeplex上的問題時遇到了很多問題,並最終放棄了它並轉到了javascript路由。 – MedicineMan 2010-01-22 23:06:12