// Demonstrates some fun analysis on Facebook data. // To use, first download the RestFB library from http://restfb.com/ // In Eclipse, right click on your project and go to Properties // Then, access Java Build Path > Libraries and click on the "Add External Jar" button // Select the library's jar file. // You'll also need a temporary access token from Facebook (see below) import java.util.*; import com.restfb.*; import com.restfb.types.*; public class FacebookGraphAPI { public static void main(String[] args) { // get your own temporary access token from // http://developers.facebook.com/tools/explorer/ String token = "MY TOKEN"; FacebookClient facebookClient = new DefaultFacebookClient(token, Version.LATEST); // quick test User me = facebookClient.fetchObject("me", User.class); System.out.println(me.getName()); Connection connection = facebookClient.fetchConnection("me/friends", User.class, Parameter.with("fields", "name, first_name, location")); System.out.println("Total friends: " + connection.getTotalCount()); // See who my friends are for (List friendList : connection) { for (User friend : friendList) { System.out.println(friend.getName()); } } // See where my friends are Map locationCounts = new HashMap(); for (List friendList : connection) { for (User friend : friendList) { if (friend.getLocation() != null) { String location = friend.getLocation().getName(); if (locationCounts.containsKey(location)) { locationCounts.put(location, locationCounts.get(location) + 1); } else { locationCounts.put(location, 1); } } } } System.out.println(locationCounts); } }