How to Fetch Facebook Graph Data with Go

I wrote a script using Go to fetch information on Facebook through their Graph API. So I want to blog about how I do it. First of all, if you’re unfamiliar with Facebook graph, we can see the information using a browser: http://graph.facebook.com/android.

A Json object will be returned:

{
   "id": "350685531728",
   "name": "Facebook for Android",
   "description": "Keep up with friends, wherever you are.",
   "category": "Utilities",
   "subcategory": "Communication",
   "link": "http://www.facebook.com/apps/application.php?id=350685531728",
   "namespace": "fbandroid",
   "icon_url": "http://static.ak.fbcdn.net/rsrc.php/v2/yo/r/OKB7Z2hkREe.png",
   "logo_url": "http://photos-b.ak.fbcdn.net/photos-ak-snc7/v43/207/227200684078827/app_115_227200684078827_474764570.png",
   "company": "Facebook"
}

Let’s start with the Go code. First, we need to fetch the content using the http.Get method from  the “net/http” package. After that, we need to close the response body when it is done. More about defer can be found here

resp, err := http.Get("http://graph.facebook.com/android")
defer resp.Body.Close()

Then we read from the resp(response) into body. More about ReadAll.

body, err := ioutil.ReadAll(resp.Body)  

    fmt.Println("resp.Body: ", resp.Body)   
    fmt.Println("body: ",string(body))
    fmt.Println("err: ",err)

After that, we need to unmarshal the the bytes[] (body) of data into a data structure so that we can use the data. This is a little tricky, so let me explain a bit. For a Json object that look like this:

{
    Name: "Alice"
    Body: "Hello",
    Time: 1294706395881547000,
}

We need to prepare a data structure like this:

type Message struct {
    Name string
    Body string
    Time int64
}

Therefore, in our case, the graph API return a Json that look like this:

{
  "id": "350685531728",
   "name": "Facebook for Android",
   "description": "Keep up with friends, wherever you are.",
   "category": "Utilities",
   "subcategory": "Communication",
   "link": "http://www.facebook.com/apps/application.php?id=350685531728",
   "namespace": "fbandroid",
   "icon_url": "http://static.ak.fbcdn.net/rsrc.php/v2/yo/r/OKB7Z2hkREe.png",
   "logo_url": "http://photos-b.ak.fbcdn.net/photos-ak-snc7/v43/207/227200684078827/app_115_227200684078827_474764570.png",
   "company": "Facebook"
}

So we need to prepare a data structure like this:

type Graph struct {
    Id string
    Name string
    Description string
    Category string
    Subcategory string
    Link string
    Namespace string
    Icon_url string
    Logo_url string
    Company string
}

*Please take note of the upper case of the first letter, it is important! See Exported Identifiers.

Finally we can use the data structure for the unmarshalling:

    var g Graph
      err = json.Unmarshal(body, &g)
    if err == nil {
      fmt.Println("graph: ", g)
    } else {
      fmt.Println("graph error: ",err) // <---error at this line
    }

Now, we can use g whichever way we like after that! 🙂

Here’s the complete and running (but messy :P) code:
https://github.com/worker8/go-lab/blob/master/fetching_graph.go