facebook real time update in java example

2.9k views Asked by At

I'm working on a project which uses Facebook Graph Api to collect information about the public posts. There is a need to update this data Real-Time when changes come to this posts. i have seen a real time update mechanism using call back url https://developers.facebook.com/docs/graph-api/reference/app/subscriptions/ and https://developers.facebook.com/docs/graph-api/real-time-updates/ .. But i didnt get any idea of doing this in java. Please somebody help me using an example.

1

There are 1 answers

0
Nikhil Ambekar On BEST ANSWER

Real time updates are available for page and user objects.Both of them requires the access token of the respective page or user.(For page you can check it using me/accounts or get it via giving page access permissions to your app).

Step 1: You will need a public ip where facebook will post the updates. Step 2: You need to setup your fb app to initiate real-time updates setup

  String appId = //your app id
  String token = //app token appId|appSecret
  String callbackUrl = //your public url
  PostMethod method = new PostMethod();
  method.setURI(new URI("https://graph.facebook.com/" + appId +
           "/subscriptions?callback_url=" + callbackUrl +
         "&object=page&fields=feed&verify_token=streamInit&method=POST&access_token="+
           token, false));

  HttpClient httpClient = new HttpClient();
  if (method != null) {
        int statusCode = httpClient.executeMethod(method);
        String response = new String(method.getResponseBody());
        if (statusCode == HttpStatus.SC_OK) {

                 //Completed streaming initiation
        }else{
           //Streaming initialization failed"
        }
     }
  } catch (Exception e) {

  }

Step 3:This will trigger facebook callback url validation.It can be a servlet or any other which will process the updates posted to callback url.Facebook will provide a challenge code in a get request which you need to resend.

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  Map parametersMap = request.getParameterMap();
  if (parametersMap.size() > 0) {
     if (request.getParameter("hub.mode").equals("streamInit")) {
        System.out.println("Verify Token: " + request.getParameter("hub.verify_token"));
        System.out.println("Challenge number:" + request.getParameter("hub.challenge"));

        String responseToClient = request.getParameter("hub.challenge");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().write(responseToClient);
        response.getWriter().flush();
        response.getWriter().close();

           //"Completed streaming setup on your url" 
     }
  } 

}

Step 3: Add your application as a page tab to subscribe from real time updates from that page.you will need a page Access token

  String appId = //your application id
  String pageId=//page id to subscribe from
  PostMethod method =
        new PostMethod("https://graph.facebook.com/" + pageId + "/tabs?app_id=" + appId +
              "&method=POST&access_token=" + pageToken);
  HttpClient httpClient = new HttpClient();
  try {
     int statusCode = httpClient.executeMethod(method);
     String response = new String(method.getResponseBody());
     if (statusCode == HttpStatus.SC_OK) {

        // "Completed tab addition"
     }else{
        // "Tab adding failed"
     }

  } catch (Exception e) {
     //"Tab adding failed" 
  }

Step 4 : Facebook will post updates via post request to your call back url.