Facebook realtime updates: Subscribing to a page

2019-07-21 16:26发布

问题:

Anyone tried subscribing to a page for real-time updates? I would like to get notified when a new wall post appears under a page's feed for example.

The Facebook documentation says this is possible but have never seen anyone accomplished this before.

https://developers.facebook.com/docs/reference/api/realtime/

回答1:

You need to get the page_accesstoken and then add the app as a tab. You can do this by getting the user access token of the admin with scope=manage_pages once you get the user access token you can query me/accounts. It will display something like

  {
      "category": "Community", 
      "name": "page name", 
      "access_token": "xxxxx", 
      "id": "1111111134678999", 
      "perms": [
        "ADMINISTER", 
        "EDIT_PROFILE", 
        "CREATE_CONTENT", 
        "MODERATE_CONTENT", 
        "CREATE_ADS", 
        "BASIC_ADMIN"
      ]
    }

That xxx would be the page access token, with the page access token you have to add your app as a tab. You can do that by

https://graph.facebook.com/PAGEID/tabs?app_id=APPID&method=POST&access_token=xxx

And now you will get a request to your callback url whenever there is a change in the page. The request looks something like.

{
    "object": "page",
    "entry": [
        {
            "id": "408518775908252",
            "time": 1360643280,
            "changes": [
                {
                    "field": "feed",
                    "value": {
                        "item": "like",
                        "verb": "add",
                        "user_id": 5900878
                    }
                }
            ]
        }
    ]
}

Hope this helps.



回答2:

Once you subscribe to realtime updates from page objects, you will get notifications only from pages which have added your app to themselves.

Much like you only get notifications from users who've auth'd your app, adding an application to a page as a tab is the equivalent of the page authing your app.

See https://developers.facebook.com/docs/appsonfacebook/pagetabs/ on how to add an app to a page.



回答3:

Try this:

try
{
  $me = $facebook->api('/me');
  $my_access_token = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=" .$fbconfig['appid'] ."&client_secret=" .$fbconfig['secret'] ."&type=client_cred"); 
  // SUBSCRIBE!   
  $subscribe = array( 'access_token'=> substr($my_access_token,13),
                      'object' => 'user',
                      'fields' => 'name,feed',
                      'callback_url' => $fbconfig['callback'], 
                      'verify_token' => $fbconfig['secret']);
  $subscribe = $facebook->api("/" .$fbconfig['appid'] ."/subscriptions", 'post', $subscribe);
  $parameters =  array("access_token" => substr($my_access_token,13) );
  $results = $facebook->api('/' .$fbconfig['appid'] .'/subscriptions', $parameters); 
}
catch (FacebookApiException $e)
{
  error_log($e);
}