Tag Archives: Graph API

Facebook Graph API – Getting Friends and Gender in C#

I recently blogged about the Facebook Graph API and if you have the Facebook C# SDK you can start making applications.

After I had my Facebook app set up, I started making a C# Console application to just get my friends and see what I could do. Here is a snippet to get my friends and their gender.

       string token = ;

            Facebook.FacebookAPI api = new Facebook.FacebookAPI(token);

            JSONObject f = api.Get("/me/friends");

            KeyValuePair friends = f.Dictionary.ElementAt(0);

            for (int i = 0; i < friends.Value.Array.Count(); i++)
                Console.WriteLine("Friend #" + i.ToString());

                JSONObject friend = api.Get("/" + friends.Value.Array[i].Dictionary["id"].String);


                catch(System.Collections.Generic.KeyNotFoundException knfe)
                    Console.WriteLine("No Gender Specified");



There is a probably a better way to do this, but getting the JSONObject back and then getting the values you get back from that, I just kind of brute forced it. Also, handling friends that don’t have information set, the quick and dirty way was to just catch the exception. I know there has to be a better way but for now it works.


Fun with the Facebook Graph API

Lazy Sunday afternoon, so I decided to dig a bit into the Facebook Graph API. What is the FB Graph API? Well it allows you to create an application and use OAuth to connect and then get information about yourself and your friends and do things in Facebook using JSON objects and requests. Easy way to read/write from Facebook.

But, the kicker seems to be getting things started. To start, Facebook has documentation and links and wikis all over the place, so it is hard to get a handle on what you want or need to do.

First you need to create an application. Once you do that you need to set up some things so you can actually use Facebook data.

Once you have that done, you can see your apps here

You will want to take note of a few things and change some settings..First might be to put your app in “sandbox” mode:

Also, under “Authentication” you might want to change Authentication Callback URLs.. and then under “Connect” change your “Connect URL”, and you want it in form http://blah.com/

So to test your app and see what you can do, you need to know your


now, you can make unauthenticated calls to the Graph API, try it..


That’s me. You should be able to see public info on me (don’t try it in IE, it pukes.. Chrome it was working. You might need to be logged into FB)

Anyways, if you want your app to be able to get more than the “public” unauthorized view, you need to make some calls and get some access tokens..

First you need to get an “access code”


You can see in that URL, you have to supply your app id, and your redirect url. Keep it simple, your redirect url should be something like http://blah.com/ .. don’t have querystring params..

If you have things set up, facebook should authenticate you, and ask you to allow your app to have permissions to pretty much “EVERYTHING” on your profile and friends info. To see what “extended permissions” you can use, see here: http://developers.facebook.com/docs/authentication/permissions

You *should* get redirect to your redirect_url with a param in the url called code=

Grab that code, you will need it.

Now to get the access_token


once you hit the above URL, with the code from step one you will get an access token

take that token, and then try


you can also get your friends ids


Once you get your friends list, you can use their id’s and get info back from the graph.

If you get errors or did something wrong, you might have screwed up your first requests. There are sites and forums saying to add type=client_cred – don’t do that, it doesn’t work. It will give you a shorter access token, which doesn’t work.

Once you have all that working *MANUALLY*, then check out Facebook’s officall C# sdk for the graph – http://github.com/facebook/csharp-sdk

Basically then you can replace the access token there with your token and test things.

Then just tie it all together in an app so you can do it programatically, but that is for another blog post :)