Real Time Updates for Facebook Page



realtime-updates


Today, I was trying to subscribe to real time updates from my Facebook pages. I had gone through all the related documents on Facebook API Docs, however, I didn't make it working. In many tutorials and docs, it was mentioned that page needs to install your application as a tab. I tried this way too. I got `200 OK` response and tab got added. However, No Luck! Sad, but I didn't find Facebook API Docs are good and clear enough.

After a long day research and hit & trails I get succeeded to get real time updates. I don't want others to suffer from the same situations. So, without taking a long, let me describe simple steps to follow to get real time updates for Facebook pages.

I will be using Graph API Explorer for describing the steps, you can follow it with any programming language.

  1. Get Page Access Token
    1.1 Get short term access token
        - Open Graph API Explorer Tool.
        - Select your application from dropdown.
        - Click on Get Access Token.
        - Check manage_pages under Extended Permissions and click Get Access Token.
        - Go to Now you need to make call to `/me/accounts`.
        - You will see a list of all accounts/pages associated with your developer account.
        - Copy Access Token  of the page that you want to subscribe for. This is the short term access token of the page which will have validity of around an hour.

    1.2 Exchange short term access token with long term access token
        - I have made it very simple for you. Just put details and click Get Long Term Access Token.









                                                  OR

        - Replace APP_ID, APP_SECRET and SHORT_TERM_ACCESS_TOKEN in the URL below.
https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=fb_exchange_token&fb_exchange_token=SHORT_TERM_ACCESS_TOKEN
       - Copy and paste the updated URL in any of the browser's address bar and hit Enter. It should return true.
        - It will return a long term access token in the format like:
          access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        - You can use Facebook's access token debug tool to check token details including validity.

 2. Page subscription to the App
    2.1 I have made it very simple for you. Just put details and click Subscribe to the page (should return true) OR get subscribed pages list (should return list of subscribed pages).








                                                  OR

       - Replace PAGE_ID and PAGE_ACCESS_TOKEN in the URL below.
https://graph.facebook.com/v2.3/PAGE_ID/subscribed_apps?method=post&access_token=PAGE_ACCESS_TOKEN
      - Copy and paste the updated URL in nay of the browser's address bar and hit Enter. It should return true.
       To validate the subscription, you can get the list of subscribed pages using following URL:
https://graph.facebook.com/v2.3/PAGE_ID/subscribed_apps?&access_token=PAGE_ACCESS_TOKEN
       It will return an array of subscribed pages.


That's it. Now whenever anything will get changed to your subscribed pages, you will get alert on specified path of your app.

Comments

  1. Sir thanks for the tutorial.
    How to specify the path of the app?

    ReplyDelete
    Replies
    1. Path is your website's URL (callback URL) on which Facebook will POST the updates. You can specify this URL while registering for real time update. You can check FB's doc here: https://developers.facebook.com/docs/graph-api/real-time-updates/v2.3 (Setting Up your Callback URL)

      Delete
    2. BTW, which programming language you are using?

      Delete
    3. Thank you sir for your answer.
      I am using PHP.
      is it necessary to add the page in the "ADD PLATFORM" (adding App as a tab to a Page)?

      Delete
    4. No, adding "Add Platform" is not mandatory.

      Delete
    5. Sir last question. So, I did the configuration right. But nothing coming as a feed back.
      This is the code I have used :
      <?php
      define('VERIFY_TOKEN', 'myString');
      $method = $_SERVER['REQUEST_METHOD'];



      if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' && $_GET['hub_verify_token'] == VERIFY_TOKEN) {

      echo $_GET['hub_challenge'];

      } else if ($method == 'POST') {

      if ( isset( $_SERVER['HTTP_X_HUB_SIGNATURE'] ) ) {
      $post_body = file_get_contents("php://input");

      if ($_SERVER['HTTP_X_HUB_SIGNATURE'] == "sha1=" . hash_hmac('sha1', $post_body, VERIFY_TOKEN)) {

      $object = json_decode($post_body, true);



      file_put_contents('updates.txt', $object , FILE_APPEND);



      }
      }
      }
      ?>

      Delete
    6. I am not sure that I understand the question, however, it looks like you are expecting some data to be posted on this callback URL. This should happen when there will be some activity occurs on subscribed entity on Facebook.

      P.S.: http://www.stackoverflow.com/ is better place to proceed this communication. You can post your question there and there will be many hands to help. I am also active on SO too: stackoverflow.com/users/1047207/raj :)

      Delete
  2. Sir what will be the varify_token?

    ReplyDelete
    Replies
    1. `verify_token` is a manually specified string. It can be anything you want.

      From FB docs:
      When your server receives one of these requests, it needs to:

      Verify the hub.verify_token matches the one you supplied when creating the subscription. This is a security check so that your server knows the request is being made by Facebook and relates to the subscription you just configured.
      Render a response to the GET request that includes only the hub.challenge value. This confirms that this server is configured to accept callbacks, and is used for security verification on Facebook's side.

      Delete
    2. Thanks for your help.
      I am new so this helps me a lot.

      Delete
    3. I am glad that it helped you. Happy Coding!

      Delete
    4. After following this tutorial i also used two urls but still no data is coming.
      1. To get App Access Token(got successfully) : https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=&client_secret=

      2. and to subscribe the app(returned srccessful): https://graph.facebook.com//subscriptions?access_token=&object=page&fields=feed&verify_token=myString&method=post&callback_url=http://www.i4mass.com/demo/fb.php


      Now got :
      {
      "object": "page",
      "callback_url": "***",
      "fields": [
      "feed"
      ],
      "active": true
      }

      but no out put is coming ....

      Delete
    5. This is expected response. It's saying that your subscription for "feed" is ACTIVE now. Nothing is wrong with it.

      Delete
  3. Thank you, Raj.
    You just saved my life.
    Now I can have real time update on my iOS app.

    ReplyDelete
  4. A big thanks from me..
    After setting callback url, I spent lots of time but finally get a perfect steps here..

    ReplyDelete
    Replies
    1. It's my pleasure that you get your things working with the help of this.

      Delete
  5. Hi,

    Thanks a lot for helping with this great post. Do you now why sometimes facebook sends same data in two or more time periods. Sometimes it is just once, but sometimes it is repeating. For example I write status on page at 22:00, I get one call at 22:02 and second on 22:03 with same data.

    ReplyDelete
    Replies
    1. From when you are facing this problem? I remember sometime earlier, Facebook had published this thing in known issues and they were working on this.

      Delete

Post a Comment