Make Facebook API Calls with FaceSharp

Development Guide Page 4 of 4
  1. Get FaceSharp with NuGet
  2. Apply Attributes on Controllers for authentication.
  3. Initialize Facebook JavaScript SDK Properly Learn how to initialize the Javascript SDK AND Facesharp Server Side at the same time.
  4. Make Facebook API Calls with FaceSharp

Graph API Calls



In FaceSharp.MVC.Code.Facebook.GraphApi there are base methods you can use to call into the Facebook Graph API, these will return a JObject if the request was sent successfully.

        
        // Get Photos in an Album
        var jObject = MakeGraphRequest(albumId, "photos");

        // Get a Photo by it's ID
        var jObject = MakeGraphRequest(photoId);

        // Get the user's details
        var jObject = MakeGraphRequest("me", fields: new[] { "id", "name", "first_name", "last_name", "email", "birthday" });

        // Get a list of all the items a user has liked
        var jObject = MakeGraphRequest(userId.ToString(), "likes");



You can easily parse the JObject into your own custom object.


           // Get a list of all the items a user has liked
           var jObject = MakeGraphRequest(userId.ToString(), "likes");

           var likes = new List<Like>();

            if (jObject != null)
            {
                var jData = (JArray)jObject["data"];
                likes = (from like in jData
                         select new Like()
                         {
                             Id = long.Parse((string)like["id"]),
                             Name = (string)like["name"],
                             Category = (string)like["category"],
                             CreatedTime = (string)like["created_time"]
                         }).ToList();

            }


Here it is all together..


        public IEnumerable<Like> GetUserLikes(long userId)
        {
            var jObject = MakeGraphRequest(userId.ToString(), "likes");
            var likes = new List<Like>();

            if (jObject != null)
            {
                var jData = (JArray)jObject["data"];
                likes = (from like in jData
                         select new Like()
                         {
                             Id = long.Parse((string)like["id"]),
                             Name = (string)like["name"],
                             Category = (string)like["category"],
                             CreatedTime = (string)like["created_time"]
                         }).ToList();

            }
            return likes.AsEnumerable();
        }

You can also work with dynamic objects as well:


  var facebookApi = new FacebookApi();
  dynamic friends = facebookApi.GraphApi.MakeGraphRequest("me", "friends", new[] {"id", "name", "first_name", "last_name"});

Rest API Calls


In FaceSharp.MVC.Code.Facebook.RestApi there are base methods you can use to call into the Facebook Rest API, these will return a string if the request was sent successfully.


// Specifying just rest method
MakeRestRequest("dashboard.decrementCount");

// Specifying both rest method and parameters
MakeRestRequest("dashboard.decrementCount", new Dictionary<string, string>()
                                                           {
                                                               {"count",count.ToString()}
                                                           });

// Executing FQL
var queryResults = _restBase.ExecuteFql("SELECT application_id FROM developer WHERE developer_id=" + _user.GetCurrentUserId().ToString());


Parsing XML returned from FQL Statement

            string results = null;
            // Execute FQL - Get Results
            var queryResults = _restBase.ExecuteFql("SELECT application_id FROM developer WHERE developer_id=" + _user.GetCurrentUserId().ToString());

            var applications = new List<Types.Application>();

            // Confirm that the results came back
            if (!String.IsNullOrEmpty(queryResults))
            {
                XNamespace ns = "http://api.facebook.com/1.0/";
                var xdoc = XDocument.Parse(queryResults);
                var appIds = from app in xdoc.Descendants(ns + "developer")
                             select (string)app.Element(ns + "application_id");
                var jObject = MakeGraphRequest(appIds);

                if (jObject != null)
                {
                    applications.AddRange(
                        appIds.Select(appId => jObject[appId]).Where(x => x != null).Select(
                        application => new Types.Application()
                        {
                            Id = long.Parse((string)application["id"]),
                            Name = (string)application["name"],
                            Description = (string)(application["description"] ?? ""),
                            Link = (string)application["link"]
                        }));
                }

            }

Last edited Dec 6, 2011 at 6:16 PM by simnova, version 21

Comments

sondlerd Sep 13, 2010 at 3:01 PM 
Got it now... i had to download the latest. It is now in the Facebook.dll assembly.
thanks

sondlerd Sep 13, 2010 at 1:47 AM 
I don't see the method MakeGraphRequest in the GraphApi class. What am I missing?

thanks