The Facebook Graph API Part 1

I’ve had a few requests for a tutorial on using the facebook graph api with my social plugin, so here it is!

My Social Unity Plugin for Android supports the Facebook Graph API. So what does that mean exactly? Well facebook expose their functionality via a rather cool but perhaps slightly confusing web interface. This interface is driven by HTTP requests and the data you get back is in the JSON format. Quickly check those links so you know what both of these are then come back here…

The Graph API is well documented and you can easily test a query by using the Graph API Explorer. So if I said you can get the current user’s profile by using a graph path of ‘me’, you can easily check this by going to the Graph API Explorer and doing this:

In my plugin you can do the same thing on the demo screen:

Or in code:

FacebookUnityPlugin.GenericRequest("me","","GET","");

In all the above cases, the response you would get will be in JSON, similar to:

{
  "id": "1234565", 
  "name": "Fred Bloggs", 
  "first_name": "Fred", 
  "last_name": "Bloggs", 
  "location": {
    "id": "111112222", 
    "name": "London, United Kingdom"
  }, 
  "gender": "male", 
  "timezone": 0, 
  "locale": "en_GB", 
  "verified": true, 
  "updated_time": "2012-07-24T09:52:15+0000"
}

So how do we extract all that information out in C# within Unity? Well the latest version of my plugin includes ‘LitJSON‘ which is remarkably good at doing this, for example if the above JSON was in a string variable ‘response’, you could get the user’s last name by:

JsonData jdata = JsonMapper.ToObject(response);
lastname = (string)jdata["last_name"];

Nice and simple! In my plugin, all responses from a GenericRequest come back via the gameobject ‘UnityGameObjectReceiver’, and you need a script attached to this gameobject that has a ‘OnRequestResponse’ method (see the demo for an example).

Part Two of this tutorial will cover more complex queries…

9 thoughts on “The Facebook Graph API Part 1

  1. Hello , I have a problem about to post a message in code , like below example:
    https://www.dropbox.com/s/j88f9ah8nl4qeym/POST.png

    I try to use a simple code
    FacebookUnityPlugin.GenericRequest(“me”, “{ \”link\” : \”http://www.platoevolved.com\”}”, “POST”, state);
    But it’s can’t work, Is this wrong?

    Another problem , what is “state” meaning?

    Sorry my English is poor!

  2. Supplement, the Response :
    (#10) Application does not have permission for this action

    I have set permissions in code
    private static string[] permissions= new string[] {“publish_stream”};

    • Hi ~ I think I found the problem,the code should be “me/feed”

      but I another problem, today I use the Social Unity Plugin for Android, It suddenly can not login. the message is:
      [Your Facebook APP Name] is misconfigured for Facebook login. Press Okay to go back to the application without connecting it to Facebook

      I’ve search it , it’s may be the hash key mismatch,
      but the Response Is no longer displayed hash key.

      It just displayed:
      android_key_not_configured:The android key hash for this application is not configured. If you are the developer of this application, visit http://www.facebook.com/developers

      Please help How can I get the hash key? THX!

      • Hi Matt, sorry for the delay, make sure you have set the app up correctly on facebook (see this video). Make sure your app on facebook is set up as a Native Android App. Hopefully the hash key will then show when you try to log in (see video). If not you will have to go the more painful route, see here for details (section 4).

        • Hi, I tried the way in your tutorial. It actually worked a few days ago. But the problem came out again yesterday. Is there any way to fix it? thx

    • Apparently you can’t use the facebook graph api to like something. However you can get the number of likes see this post for details. Let me know how you get on.

    • Sorry for the delay, only just noticed this, you probably have the solution since it’s very easy. Just use a graph path of ‘me/friends’. More information here.

Leave a Reply

Your email address will not be published. Required fields are marked *