Implementing Facebook Real-time updates API (with CURL examples)
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!
Hi!
Thanks so much for this post. I really needed to see just a simple concrete example.
I have 2 small questions:
1. What is client_cred from step 1?
2. If I already have a valid access token, I can skip steps 1&2 right? I don’t need to get another access token especially for subscribing to updates right?
Thanks alot!
Rachel
July 18, 2010 at 3:08 pm
@Rachel:
1) you can use “client_cred” as a literal string. No substitution needed. I think it stands for client credentials. See http://developers.facebook.com/docs/authentication/ for more details.
2) Yes, you can skip steps 1&2 if you have a valid access token. Please note, tho’, that this access token is different from an access token granted to a *user* of your app. This is really an access token granted to the administrator of the app (hence the need to specify your app secret in step (1).
Chao
July 18, 2010 at 6:01 pm
Hi, i am getting empty json string when receive any real time updates. Why this will happen? Help.. thx.
falcon
November 23, 2010 at 6:51 am
I am using C# to send the request and using PHP as a callback server. Also, i have allow all extended permission.
falcon
November 23, 2010 at 6:52 am
Iam new to curl. Can u give sample php prog to do this.
aparna.m
December 10, 2010 at 10:11 am
This is awesome.
For how long is the token valid?
Forever?
Also; what is the you’re using in the final curl command?
The access token, or the app secret?
Thank you very much!
Jonathan S
January 11, 2011 at 9:34 pm
@Jonathan:
Not sure how long the access token is valid, but I know the javascript access token is valid up at least 2 hours, so I’d imagine it’s the same or similar.
As to the second question, if I understand it correctly (I think you missed typing something), the final curl example uses both the access token and the secret token as indicated.
Chao
January 12, 2011 at 9:41 pm
Really a nice one
Thanks a lot
manish sharma
January 15, 2011 at 1:17 pm
Hey where does the access token get returned to on the first step…?
I’ve spent the past couple of days trying to get access tokens using the authorization code exchange process and to no avail.
Any help on how either way works would be a great help. Maybe a complete php file on how its implemented.
I am quite new to php and brand new to the facebook api etc..
Thanks for your time.
Fintan
January 23, 2011 at 5:36 pm
@Fintan: the access token is returned as a string by Curl in the example. An example is shown in the bottom of step 1.
If you’re new to RESTful apis, one suggestion is to familiarize yourself with the HTTP protocol e.g. read up http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
Chao
January 23, 2011 at 5:49 pm
Super . Fabulous. Keep it up. Thanks a billions !!!!
Dheeraj chouhan
February 4, 2011 at 5:58 am
Question.
In step 1. I’m getting the {“error”:{“type”:”OAuthException”,”message”:”Missing redirect_uri parameter.”}}
How is this possible?
Zvonimir
March 29, 2011 at 10:52 am
So to understands this?
You can make an app that gets the user_checkins permission of users.
Subscribe to the checkins.
And when any user who is allowing the app does a checkin, your app gets a POST notififcation on the callback url with the checkin info?
Do you also need to offline_access permission?
The verify_token= secret token in the last part? This is sent as a get variable back to your url so that you can be sure that facebook is calling you script not some loser…?
Rob
May 5, 2011 at 3:32 pm
Hi I am Korean.
Do I understand English is a bit lacking.
Your reading was very helpful. Thanks.
I do not know how to save on verify_token.
It asks for a detailed answer.
Seongyong Kim
June 7, 2011 at 9:11 am
[...] I found a blog post explaining how to do this, here it [...]
Subscribing to Facebook Graph API’s real time updates using CURL. « Random Ramblings!
July 5, 2011 at 9:51 pm
I never heard or used cURL.
Where do I put it? How?
Paul
July 18, 2011 at 8:46 pm
U Have made my day sir…..
thanks a ton…..
Vinay
January 9, 2012 at 11:56 am
@Chao:
Im using java,
I have tried a variety of code to subscribe,
but not able to….
pls kindly help me … or suggest some site which help s subscribing…
Vinay
January 11, 2012 at 5:22 am
Vinay, all you need is a java library that allows you to make HTTP calls, better REST client HTTP library. You can then translate all these CURL commands to api calls to that library.
You do need a basic understanding of the HTTP protocol which you can easily google around (the beauty of HTTP is in its simplicity)
Chao
January 11, 2012 at 6:09 am
Thanks for the reply and your time .
will look into it.
Cheers
Vinay
January 11, 2012 at 7:40 am