Skip to content

Instantly share code, notes, and snippets.

@timohausmann
Created January 20, 2014 19:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timohausmann/8527421 to your computer and use it in GitHub Desktop.
Save timohausmann/8527421 to your computer and use it in GitHub Desktop.
Another Facebook Graph API Example for Processing
/**
* In this example, we will request your first name and your profile picture from Facebook.
* We will store the acquired information in these variables:
*/
String name;
int mutualfriends_amount;
int likes_amount;
/**
* You need an Access Token to perform requests to the Facebook Graph API.
* Access Tokens are only valid for a short period of time (1-2 hours).
* Generate your Access Token here: https://developers.facebook.com/tools/explorer
* Learn more about Access Tokens: https://developers.facebook.com/docs/facebook-login/access-tokens/
*/
String access_token = "CAACEdEose0cBAN95CVGmrlMaERX98VrAQbmZBFXCHH7uOhSdg6pYSfgeIMwOs13cxiO82I6D9AfWmPZAxrA0QV2hYnZB49Ayal4TfYSdGjHpCq1WpbYE5ajltk9AZAkVFl1oRTfA46YsYnMsVy171elEeqT1SrFFHRlIh7dDQfKtIZAOwBJPp3gOvCwhUZBzwZD";
void setup() {
size(640, 480);
background(255);
/**
* You can now perform Facebook Graph API requests using the custom function FB().
* We will ask Facebook about your first name and your picture.
* Learn more about Facebook Graph API: https://developers.facebook.com/docs/graph-api/quickstart/
*/
JSONObject response = FB("timo.hausmann?fields=first_name,mutualfriends.limit(999),likes.limit(999)");
/**
* Lets take a look at the data we got from Facebook in the console:
*/
println(response);
/**
* We can now extract the information we want from the response.
* Learn more about JSONObject: http://www.processing.org/reference/JSONObject.html
*/
name = response.getString("first_name");
mutualfriends_amount = response.getJSONObject("mutualfriends").getJSONArray("data").size();
likes_amount = response.getJSONObject("likes").getJSONArray("data").size();
}
void draw() {
fill(255,255,255);
rect(0,0,width,height);
fill(0);
/**
* Draw the data
*/
text( "Name: " + name, 50, 50 );
text( "Anzahl Gemeinsame Freunde: " + mutualfriends_amount, 50, 74 );
text( "Anzahl Page Likes: " + likes_amount, 50, 98 );
}
/**
* Send a request to the Facebook Graph API
* @param String request The Facebook Graph API Request
* @return JSONObject The Facebook Graph Data
*/
JSONObject FB( String request ) {
String json_string = "";
String connector = (request.indexOf("?") == -1) ? "?" : "&";
String response[] = loadStrings("https://graph.facebook.com/" + request + connector + "access_token=" + access_token);
for(int i=0;i<response.length;i++) {
json_string += response[i];
}
JSONObject json = JSONObject.parse(json_string);
//println("==============");
//println("Request: " + request);
//println("Response: " + json);
//println("==============");
return json;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment