0

Getting back onto facebook c# sdk and always find some changes here and there. GOt about accessing an access token properly and now i want to count number of friends for user.

Here is my existing code.

string accessToken = ViewState["accessToken"].ToString();
Facebook.FacebookClient fb = new FacebookClient(accessToken);
var me = fb.Get("/me/friends");

Now the me gives me the friend list in json format which has two keys one being "data" and other being "paging".

How do i proceed to get all friends and at least count of friends?

Thanks for the inputs

Mandar Jogalekar
  • 3,199
  • 7
  • 44
  • 85

2 Answers2

2

I've used the FB's Graph API Explorer and there are additional url parameters you can add to this query, i.e. "limit=number" and "offset=number". It seems that when you don't specify those then FB will return the MAX allowed number of friends (5000). It will still give you the "paging" key, but when you follow the link there are no friends on the 2nd page.

You can do:

var me = fb.Get("/me/friends?limit=5000&offset=0");

to assure that the default limit is not changed to some small value in the future.

So the answer to your question is:
Use the query above, don't worry about the paging, parse JSON returned from FB and count the number of objects inside "data" array. More defensively you can also make a request to the paging.next and follow that URL to check if there are any additional friends on the next page (which should not happen, as there's the 5000 limit).

Adrian Ciura
  • 1,102
  • 1
  • 9
  • 14
  • yes i get the data properly.. important step next is how do i go about deserializing it as i dont find any deserializing option in fb client. – Mandar Jogalekar Oct 28 '12 at 11:23
  • See this SO answer for [JSON Deserialize in C#](http://stackoverflow.com/questions/7895105/json-deserialize-c-sharp) – Adrian Ciura Oct 28 '12 at 11:34
2

Thankfully got around it myself.. here is how. hopefully it ll save someone lot of headbanging got it from this link

Facebook dynamic object

here is my code which i guess would also be good for likes/comments etc.

 dynamic friends = fb.Get("/me/friends?limit=5000&offset=0");

            Dictionary<string, string> resultList = new Dictionary<string, string>();



            foreach (var item in friends.data)
            {
                var newdata=item;
                string item1 = newdata[0].ToString();
                string item2 = newdata[1].ToString();

                if (resultList.ContainsKey(item1))
                {
                    item1 = item1 + "duplicate";  
                }

                resultList.Add(item1,item2);
            }

that containsKey is to avoid throwing exception as some nerds have profiles with same name..

Last thing here is it still returns me the lesser than my actual friend list count no idea why.. :(

Mandar Jogalekar
  • 3,199
  • 7
  • 44
  • 85