Home > BigData, HOWTOs > Getting Batch Data from Facebook using restFB APIs

Getting Batch Data from Facebook using restFB APIs

Here is quick sample code to get data from Facebook Batch API.

Download the jar from here – http://code.google.com/p/restfb/downloads/detail?name=restfb-1.6.9.zip

And put it in your library path and execute the below code.

Go to this link and login to facebook to get your access token : https://developers.facebook.com/tools/explorer

Change the code to pass your “AccessToken” directly to “DefaultFacebookClient facebookClient = new DefaultFacebookClient(“<<>>”);

 

import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.DefaultJsonMapper;
import com.restfb.Facebook;
import com.restfb.FacebookClient;
import com.restfb.JsonMapper;
import com.restfb.Parameter;
import com.restfb.batch.BatchRequest;
import com.restfb.batch.BatchRequest.BatchRequestBuilder;
import com.restfb.batch.BatchResponse;
import com.restfb.json.JsonArray;
import com.restfb.json.JsonObject;
import com.restfb.types.Page;
import com.restfb.types.Post;
import com.restfb.types.Url;
import com.restfb.types.User;

public class GraphReaderHadoopFB {

  public static void main(String[] args) {

      DefaultFacebookClient facebookClient = new DefaultFacebookClient(“<<>>”);
     
          out.println(“Starting BATCH \n”);
     
          //Building Batch Request to send to Facebook
          out.println(“Creating BATCH \n”);
        BatchRequest meRequest = new BatchRequestBuilder(“me”).build();
        BatchRequest meFriendRequest = new BatchRequestBuilder(“me/friends”).build();
        BatchRequest meLikeRequest = new BatchRequestBuilder(“me/likes”).parameters(Parameter.with(“limit”, 5)).build();
       
       
        //Creating POST Request – Not working yet – moved to GET
        out.println(“Posting Request \n”);
        BatchRequest postRequest = new BatchRequestBuilder(“me”).method(“GET”)
                .body(Parameter.with(“message”, “Info!!!”)).build();

        //Executing BATCH Request
        out.println(“Complete Batch Response \n”);
        List batchResponses =
                facebookClient.executeBatch(meRequest, meFriendRequest, meLikeRequest, postRequest);
       
       
        //Got Response we can use this information to process further.
        out.println(“\n Response \n”);
        BatchResponse meResponse = batchResponses.get(0);
        BatchResponse meFriendResponse = batchResponses.get(1);
        BatchResponse meLikeResponse = batchResponses.get(2);
        BatchResponse postResponse = batchResponses.get(3);
       
        out.println(“\n *********** Individual Reponse ************* \n”);
       
        out.println(“\n meResponse \n”);
        out.println(meResponse.getBody());
       
        out.println(“\n meFriendResponse \n”);
        out.println(meFriendResponse.getBody());
       
        out.println(“\n meLikeResponse Getting 5 (LIMITED) \n”);
        out.println(meLikeResponse.getBody());
       
        out.println(“\n postResponse \n”);
        out.println(postResponse.getBody());
       
  }
}

Advertisements
Categories: BigData, HOWTOs
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: