Chao Lam

Working on the next small things …

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!

Advertisements

Written by Chao

June 7, 2010 at 6:39 pm

Posted in Uncategorized

29 Responses

Subscribe to comments with RSS.

  1. 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

  2. @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

  3. 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

  4. 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

    • I am using C# for my app. But i don’t know how to update real-time. How do you use PHP to do this?

      bu0i

      April 13, 2012 at 2:06 am

  5. Iam new to curl. Can u give sample php prog to do this.

    aparna.m

    December 10, 2010 at 10:11 am

  6. 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

    • if you set “scope” parameter as “offline”, the acces_token is valid for forever. In normal cases is valid upto 30 min (max)

      nagendra

      May 16, 2012 at 5:24 am

  7. Really a nice one
    Thanks a lot

    manish sharma

    January 15, 2011 at 1:17 pm

  8. 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

  9. @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

  10. Super . Fabulous. Keep it up. Thanks a billions !!!!

    Dheeraj chouhan

    February 4, 2011 at 5:58 am

  11. 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

  12. 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

  13. 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

  14. […] I found a blog post explaining how to do this, here it […]

  15. I never heard or used cURL.
    Where do I put it? How?

    Paul

    July 18, 2011 at 8:46 pm

  16. U Have made my day sir…..
    thanks a ton…..

    Vinay

    January 9, 2012 at 11:56 am

  17. @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

      • Hi Chao,
        Do you Think a code like this is good enough??

        code begins:

        try {
        // Construct data
        String data = URLEncoder.encode(“key1”, “UTF-8”) + “=” + URLEncoder.encode(“value1”, “UTF-8”);
        data += “&” + URLEncoder.encode(“key2”, “UTF-8”) + “=” + URLEncoder.encode(“value2”, “UTF-8”);

        // Send data
        URL url = new URL(“http://hostname:80/cgi”);
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
        // Process line…
        }
        wr.close();
        rd.close();
        } catch (Exception e) {
        }

        :Code Ends

        Thanks

        Vinay

        January 11, 2012 at 10:08 am

  18. @Chao:
    Hi chao.
    I got the response as “null” as mentioned above.Thanks a ton for your advice.
    Now what am I supposed to do , to check if my subscription is validated or not.
    kindly help me to go about it further.
    Thanks

    Vinay

    February 6, 2012 at 7:49 am

  19. @chao: after all this step. what is next step.
    how to handle update in callback.php page..
    need your reply..
    paste your callback page code here if possible,

    Anand Gajajr

    April 3, 2012 at 7:36 am

  20. You’ve given excellent directions, I’ve followed all of these steps, everything is verified: I can list subscriptions, FB definitely GETs the endpoint page challenge response.

    Still FB does not POST anything. I’ve waited days. It seems like this excellent tutorial is not yet complete? What are the necessary conditions to actually get FB to post? The documentation says that a user must have installed your app, and a page must have your app as a tab on its page, and you must be subscribed to the relevant fields, and the fields must change. All of these conditions exist, and still no update.

    Rendall

    June 25, 2012 at 12:41 pm

  21. hey Chao
    I am very new to this. I have created a facebook application and have accessed user’s friend list. But I want to update it regularly.
    First 2 steps are working fine for me but i dont know how to implement the last step.
    Please help me out.
    Thank you.

    Shashank

    June 26, 2012 at 10:58 am

  22. Hi Rendall, I really need this to work, I followed all instructions from facebook docs and all pages related to this but still cannot get FB to send me updates, did you ever get this working?

    David

    October 9, 2014 at 5:17 am

  23. Hi Chao
    I have subscribed for page “object” with field as “feed” & the subscription is successfull, but I am unable to recieve any realtime update for post or comments of the page, can u please help me out with this

    Ganesh Deshvini

    December 10, 2014 at 4:43 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: