Chao Lam

Working on the next small things …

Archive for June 2010

Woohoo!

leave a comment »

YouTube wins case against Viacom.

Advertisements

Written by Chao

June 24, 2010 at 3:24 am

Posted in Uncategorized

Implementing Facebook Real-time updates API (with CURL examples)

with 29 comments

I recently implemented Facebook’s ¬†real-time updates api. Since I couldn’t find a decent tutorial and ran into a few small roadblocks, I thought I’d share what I learnt.

This tutorial basically gets real-time updates of any name changes for users of your FB app. I’m going to use examples with Curl – since that’s like the swiss knife of http programming.

Step 1 – get access token


curl https://graph.facebook.com/oauth/access_token?client_id=<app-id>&client_secret=<app-secret>&type=client_cred

Note you can also use your api key (which I accidentally did!)

This should return a 40 over character long access token, the initial part looks suspiciously like your app id e.g.

access_token=222868257269|34567890123456789012345678901

Step 2 – Check if access token is working (optional)

With access token in hand, you can now query what real-time updates your app is already subscribed to. Real-time updates API is RESTful; so a HTTP Get returns your current subscriptions. e.g.


curl "https://graph.facebook.com/<appid>/subscriptions?access_token=<access token from  step (1)>"

This result is JSON-encoded. If you’re just starting out, you would have no subscriptions:


{"data":[]}

If you somehow goofed up, a JSON-encoded error is returned:


{"error":{"type":"Exception","message":"xxx"}}

Note: api key can’t be used instead of app id here!

Step 3 – Submit your update

This is the meat of the call. You are to submit a HTTP POST with Form-like arguments (don’t POST with JSON-encoded arguments as I had wrongly assumed)

curl -F 'object=user' \
-F 'callback_url=<callback url you have to implement>' \
-F 'fields=name' \
-F 'verify_token=<secret token>' \
"https://graph.facebook.com/<appid>/subscriptions?access_token=<access token from (1)"

curl should return “null”. Go back to Step (2) to check if all you entered was recorded by Facebook.

Hope this helps!

Written by Chao

June 7, 2010 at 6:39 pm

Posted in Uncategorized