views:

981

answers:

2

Starting from this very nice article about doing facebook integration using the javascript API, I was able to successfully make several calls against the FB.Connect namespace.

I am now interested in calls that are in the FB.ApiClient library, and have tried a few simple ones. I cannot get this to work, is there something I have to do that different? Specifically, I would like to make calls against the events_get methods.

Documentation:

Code:

Working JavaScript:

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

Error: FB.ApiClient.get_apiKey is not a function

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

Is FB.ApiClient defined? Have you tried wrapping it in a FB_RequireFeatures call, and making sure it's initialised? E.g.

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)
                });
        });
Karl B
It appears to be defined. I changed the capitalization on ApiClient to apiClient, and the new error is that FB.apiClient is undefined. I tried using your code above, but I got the same error. Substituted 'api_key' for my own api_key, and path for my own path.
MedicineMan
I've edited my answer to something that I've managed to get working for the friends_get api call - so any of the others should follow.
Karl B
thanks for posting that. Any idea why you call FB.Connect.streamPublish vs FB.Facebook.apiClient.friends_get? That was really confusing and pretty darn frustrating. I saw that you work for SocialCash. I saw that SocialCash is maintaining a .NET wrapper for Facebook. How is their .NET wrapper as compared to the one on: www.codeplex.com/FacebookToolkit? I had a lot of problems using the one on codeplex and eventually just dropped it and went the javascript route.
MedicineMan
We've been winding down the .Net wrapper ever since Microsoft released a maintained official library: http://wiki.developers.facebook.com/index.php/Microsoft_SDK_for_Facebook_Platform
Karl B
+1  A: 

the namespace and methods are inconsistent in the way that they are named and used. To access the apiClient, you must use FB.Facebook.apiClient.

In short. Instead of calling:

FB.apiClient.get_apiKey();

call:

FB.Facebook.apiClient.get_apiKey();

Below, I have included a complete example, where I worked out several methods on the Connect and ApiClient APIs. This is working as of 1/22/09.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml"&gt;
<body>
<a target="_blank" href="http://developers.facebook.com/docs/?u=facebook.jslib"&gt;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&amp;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&amp;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"&gt; </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>
MedicineMan