Last active
March 20, 2019 23:05
-
-
Save timohausmann/8526948 to your computer and use it in GitHub Desktop.
Simple Facebook Graph API Example With Processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 my_name; | |
PImage my_photo; | |
/** | |
* 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 = "CAACEdEose0cBADKyneBQz0beBnXEuJXoFfCG2jk3v8pjDvVZAcgCTdtGEGkpONwFDGuuukwZA5BuehOx7vVplVy7a22m6r61mzCB6SIjhmYPeuvaXC78mhVlpJ95FWuswVu5RAZAJSc0FgGmFhb8aIa4X4VB9I63yVofIBSTeQHn7umZCkjLfly5WyLlvsUZD"; | |
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_me = FB("me?fields=first_name,picture"); | |
/** | |
* Lets take a look at the data we got from Facebook in the console: | |
*/ | |
println(response_me); | |
/** | |
* We can now extract the information we want from the response. | |
* Learn more about JSONObject: http://www.processing.org/reference/JSONObject.html | |
*/ | |
my_name = response_me.getString("first_name"); | |
my_photo = loadImage( response_me.getJSONObject("picture").getJSONObject("data").getString("url") ); | |
} | |
void draw() { | |
fill(255,255,255,10); | |
rect(0,0,width,height); | |
fill(0); | |
/** | |
* Draw the name and profile picture at random positions | |
*/ | |
text( my_name, random(width), random(height) ); | |
image( my_photo, random(width), random(height) ); | |
} | |
/** | |
* 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