George Profenza
George Profenza

Reputation: 51837

How to easily use the Facebook API Realtime Updates feature?

First off, apologies for posting yet another question on Facebook Realtime Updates. I have read many existing stackoverflow answers and useful articles which helped, but I still can't seem to figure out how to put everything together.

All I'm trying to do is get a trigger when there's a user or page updates (be it status/comment/like/etc.) in realtime.

I started with the Realtime Updates documentation and found these two blog posts handy:

From what I understood, to register for Realtime Updates, I need to:

  1. Create a WWW Facebook app
  2. Point the Facebook app to a callback_url
  3. Add a php script at the callback_url to handle a GET request (for verification) and POST requests when Facebook calls.
  4. Register the callback with the Graph API (v2.3/APP_ID/subscriptions)
  5. Add the Facebook Login button to the page (including the scope/permissions needed) and perform the login action

In theory, after this point, Facebook should POST to the callback_url based on the registered object and fields.

I think I've successfully registered the callback. Here roughly the output I get(with MY_CB_URL replacing the actual URL):

{
  "data": [
    {
      "object": "user", 
      "callback_url": "MY_CB_URL", 
      "fields": [
        "statuses"
      ], 
      "active": true
    }, 
    {
      "object": "page", 
      "callback_url": "MY_CB_URL", 
      "fields": [
        "feed"
      ], 
      "active": true
    }
  ]
}

The callback php script looks like so:

<?php

define('VERIFY_TOKEN', 'vToken');

$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') {

    $out = "";

    try {

        $updates = json_decode(file_get_contents("php://input"), true);

        $out = print_r($updates, true);

        error_log('updates = ' . $out);
    } catch (Exception $e) {
        error_log('Caught exception: '.$e->getMessage());
        $out = $e->getMessage();
    }

    $file = './log.txt';
    $current = file_get_contents($file);
    $current .= $out;
    file_put_contents($file, $current);

}

?>

The problem I have is I got a single POST request when I first set this up, but none after that. I don't get any errors and the API confirms the callback is correctly registered, so I am clueless on what I may be missing.

I've spotted this answer and make a call as suggested to

https://graph.facebook.com/PAGE_ID/tabs?app_id=APP_ID&access_token=PAGE_ACCESS_TOKEN

and got this response:

{
   "data": [
      {
         "id": "PAGE_ID/tabs/likes",
         "name": "Likes",
         "link": "https://www.facebook.com/pages/PAGE_NAME/PAGE_ID?sk=likes",
         "is_permanent": true,
         "position": 2,
         "is_non_connection_landing_tab": false
      },
      {
         "id": "PAGE_ID/tabs/photos",
         "image_url": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xaf1/t39.2080-0/851586_10151609549247733_1069686154_n.gif",
         "name": "Photos",
         "link": "https://www.facebook.com/pages/PAGE_NAME/PAGE_ID?sk=photos",
         "application": {
            "name": "Photos",
            "id": "PHOTO_ID"
         },
         "is_permanent": false,
         "position": 1,
         "is_non_connection_landing_tab": false
      }
   ]
}

So now I'm further confused.

Upvotes: 2

Views: 1342

Answers (1)

C3roe
C3roe

Reputation: 96226

Making a POST request to https://graph.facebook.com/PAGE_ID/tabs is outdated – you need to use /PAGE_ID/subscribed_apps now to subscribe to updates from a page.

https://developers.facebook.com/docs/graph-api/reference/page/subscribed_apps/

Upvotes: 2

Related Questions