Contact Us

Contact Us



Pleas confirm by checkbox


Technical

FACEBOOK -RECEIVING PAGE UPDATES IN REAL TIME WITH WEBHOOKS

Author_img
By Manas Chamuah February 23, 2017

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

Related posts
Apache Spark Standalone Setup On Linux/macOS
Technical

Apache Spark Standalone Setup On Linux/macOS

By manasprat October 20, 2021
Apache Flink Standalone Setup on Linux/macOS
Technical

Apache Flink Standalone Setup on Linux/macOS

By manasprat October 13, 2021
Identity, Authentication, And Access Management Using Azure Active Directory  
Technical

Identity, Authentication, And Access Management Using Azure Active Directory  

By manasprat September 22, 2021
Things to Know Before You Select A Crypto Wallet
Blockchain

Things to Know Before You Select A Crypto Wallet

By manasprat September 20, 2021
Solve 3 Most Irritating Outlook Email Rendering Issues.
Technical

Solve 3 Most Irritating Outlook Email Rendering Issues.

By manasprat September 15, 2021
Intuit Wasabi – A Scalable A/B Testing Solution
Technical

Intuit Wasabi – A Scalable A/B Testing Solution

By manasprat September 01, 2021
How To Pick The Right Data Analytics Strategy For Serverless Systems?
Big Data

How To Pick The Right Data Analytics Strategy For Serverless Systems?

By manasprat August 25, 2021
Change Notifications and Named Options using Options pattern in .NET Core
Technical

Change Notifications and Named Options using Options pattern in .NET Core

By manasprat August 18, 2021
Create Strongly Typed Configurations in .NET Core
Technical

Create Strongly Typed Configurations in .NET Core

By manasprat August 13, 2021
Partitioning Database - A Divide and Rule Strategy
Technical

Partitioning Database - A Divide and Rule Strategy

By manasprat June 02, 2021

Stay updated

Get the latest creative news from Fubiz about art, design and pop-culture.