FACEBOOK -RECEIVING PAGE UPDATES IN REAL TIME WITH WEBHOOKS

PROMOTING YOUR CAMPAIGN OR EVENT

Facebook is the most powerful  tool  in one’s arsenal for promotional purposes. It is responsible for eliciting more contributions to campaigns/events/fan-following than any other social networking tool.(not all may agree though).

So you create a facebook page and want to do analytics on its feeds and post, for that you may want to subscribe to it and  get it’s real time updates to evaluate how you are performing. But alas it’s official api documentation is in Klingon, but as always, deciphering Facebook’s documentation is a form of art. So in this blog I will try to guide you through the same.

Official documentation

https://developers.facebook.com/docs/graph-api/webhooks

CREATE A FACEBOOK APP

The first step to proceed to your goal,you need to create a facebook App and use its app ID and secret key to create a permanent token for the feed.

Follow the steps as specified in the link

https://developers.facebook.com/docs/apps/register

ADD YOUR APP TO YOUR PAGE’S SUBSCRIBED APPS

To receive live updates about comment/likes/post in your application you will need to add the app to your page’s subscribed apps.

If you don’t have any application around your facebook app you can use the Graph Explorer of facebook.Select your app in place of Graph Explorer

facebook App

1. Scroll to 'Graph API Explorer' title header
2. There will be a 'Application' text with dropdown besides it
3. Choose your application from the dropdown 
 NOW SEND A POST REQUEST USING PAGE ACCESS TOKEN TO THE FOLLOWING URL
{page_id}/subscribed_apps

You will get a success response for the same.

{

success: bool,
messaging_success: bool

}

REGISTERING  A CALLBACK URL FOR SUBSCRIPTION

Facebook will make a request to that URL from their servers to deliver the updates – so of course it has to be publicly reachable over the internet.

When you add a new subscription, or modify an existing one, Facebook servers will make a GETrequest to your callback URL in order to verify the validity of the callback server. A query string will be appended to this URL with the following parameters:

  • hub.mode – The string “subscribe” is passed in this parameter
  • hub.challenge – A random string
  • hub.verify_token – The verify_token value you specified when you created the subscription

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. This value is only passed on the initial API call toAPP_ID/subscriptions.
  • 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.

In Java(Spring) :

@RequestMapping(value ="/facebookPage", method = RequestMethod.GET)
public String getfacebookChallengePage(@RequestParam(value = "hub.mode") 
String mode,
 @RequestParam(value = "hub.verify_token") String verify_token,
 @RequestParam(value = "hub.challenge") String challenge) {

if(mode.equalsIgnoreCase("subscribe") && verify_token.equalsIgnoreCase("token")){
 return challenge;
 }
 
 return "Challenge Failed";

}
//To receive the updates
@RequestMapping(value = "/facebookPage", method = RequestMethod.POST)
 public void facebookDataStreamPage(HttpServletRequest request,String data)
throws Exception {

String pushedJsonAsString = IOUtils.toString(request.getInputStream(),"utf-8");
 System.out.println("JSON String : "+pushedJsonAsString);

}

For Node.js

app.get(['/facebook', '/instagram'], function(req, res) {
 if (
     req.param('hub.mode') == 'subscribe' &&
     req.param('hub.verify_token') == 'token'
 ) {
    res.send(req.param('hub.challenge'));
 } else {
    res.sendStatus(400);
 }
});

app.post('/facebook', function(req, res) {
   console.log('Facebook request body:');
   console.log(JSON.stringify(req.body))
   console.log('Facebook request body end:'); 
   // Process the Facebook updates here
   res.sendStatus(200);
});

One of the hurdles one may face(if you don’t have SSL certificates) is that the callback URL accepts only https endpoint,so for that you can deploy your application in Heroku,which will provide you an https endpoint.

ALL SET READY TO GO !!

Now after performing the above steps ,updates from your page (comments, likes, and new posts) will be received at your registered callback URL.

Code on….

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