This document provides an overview of the Facebook Graph API. It begins with an introduction to the Graph API and what kind of data is available through it, with or without authentication. It then discusses the data structures and objects in the Graph API, including the different types of objects and how they are connected. The document also covers how to connect to the Graph API from JavaScript and ActionScript and discusses methods for retrieving, posting, and modifying data programmatically through the Graph API.
1. Me.
James Ford
http://www.psyked.co.uk/
@psyked_james
Product Development @ MMT Digital
Web (HTML, CSS, JavaScript)
Flash Platform (Flash, Flex, AIR)
Mobile (Obj-C, ELIPS, Titanium, Corona SDK)
2. Flash Facebook Cookbook
Published August 2011
Packt Publishing
- Facebook Graph API 1.6
- FQL
- , & ,
- Introduction to Facebook
Graph API, core concepts
& lots of simple demo
applications.
3. Initial questions
• What data is available from Facebook?
• How is that data structured?
• How do we connect to the Graph API?
• How do we locate information on Facebook?
• What data-access controls are in place?
10. Facebook data types
Common types And the less common...
• Album • Insights
• Application • Message
• Checkin • Note
• Comment • Question
• Event • QuestionOption
• Group • Review
• Link • Subscription
• Page • Thread
• Photo
• Post
• Status message
• User
• Video
11. Graph API Objects
Everything has a unique Id, and can be accessed directly:
• Users: https://graph.facebook.com/btaylor (Bret Taylor)
• Pages: https://graph.facebook.com/cocacola (Coca-Cola page)
• Events: https://graph.facebook.com/251906384206 (Facebook Developer Garage Austin)
• Groups: https://graph.facebook.com/195466193802264 (Facebook Developers group)
• Applications: https://graph.facebook.com/2439131959 (the Graffiti app)
• Status messages: https://graph.facebook.com/367501354973 (A status message from Bret)
• Photos: https://graph.facebook.com/98423808305 (A photo from the Coca-Cola page)
• Photo albums: https://graph.facebook.com/99394368305 (Coca-Cola's wall photos)
• Profile pictures: https://graph.facebook.com/psyked/picture (your profile picture)
• Videos: https://graph.facebook.com/817129783203 (A Facebook tech talk on Graph API)
• Notes: https://graph.facebook.com/122788341354 (Note announcing Facebook for iPhone 3.0)
• Checkins: https://graph.facebook.com/414866888308 (Check-in at a pizzeria)
12. Graph API Connections
Graph API Connections are used to represent relationships in
the ‘social graph’, and return a JSON-encoded Array of objects.
• Friends: https://graph.facebook.com/me/friends
• News feed: https://graph.facebook.com/me/home
• Profile feed (Wall): https://graph.facebook.com/me/feed
• Likes: https://graph.facebook.com/me/likes
• Books: https://graph.facebook.com/me/books
• Permissions: https://graph.facebook.com/me/permissions
• Photo Tags: https://graph.facebook.com/me/photos
• Photo Albums: https://graph.facebook.com/me/albums
• Video Tags: https://graph.facebook.com/me/videos
• Video Uploads: https://graph.facebook.com/me/videos/uploaded
• Events: https://graph.facebook.com/me/events
• Groups: https://graph.facebook.com/me/groups
• Checkins: https://graph.facebook.com/me/checkins
13. A Public Graph API Object
GET http://graph.facebook.com/platform
{
"id": "19292868552",
"name": "Facebook Platform",
"picture": "http://profile.ak.fbcdn.net/hprofile-ak-
ash2/276791_19292868552_1958181823_s.jpg",
"link": "https://www.facebook.com/platform",
"likes": 3403793,
"category": "Product/service",
"website": "http://developers.facebook.com",
"username": "platform",
"founded": "2007",
"company_overview": "Facebook Platform enables anyone to build social apps
on Facebook and the web.",
"mission": "To make the web more open and social."
}
29. Requesting Permissions
var options:Object = {};
options.perms = new Array("user_about_me", "email", "friends_birthday");
Facebook.login(loginResponseHandler, options);
private function loginResponseHandler(success:Object, fail:Object):void {
if (success) {
Facebook.api('/platform', requestResponseHandler);
} else {
trace('Unable to log in');
}
}
30. Checking for Permissions
var missingPermissions:Array = new Array();
private function requestPermissionsInfo():void {
var permissions:Array = new Array("read_stream", "publish_stream");
Facebook.fqlQuery("SELECT {0} FROM permissions WHERE uid = me() ",
onFQLResponse, [permissions.toString()]);
}
private function onFQLResponse(success:Object, fail:Object):void {
[{
missingPermissions = new Array();
"email":0,
var results:Array = success as Array;
"publish_stream":1,
if (success && results) { "read_stream":1
for (var i:int = 0; i < results.length; i++) {
}]
var queryResult:Object = results[i];
for (var param:String in queryResult) {
if (Boolean(queryResult[param]) == false) {
missingPermissions.push(param);
}
}
}
}
}
33. Posting data to the Graph API
var options:Object = new Object();
options.name = "I found a trophy";
options.caption "100001986549853_202809986461885"}
{"id": = "Gold trophy";
options.description = "I found a Gold trophy while using ...";
options.link = "http://apps.facebook.com/packt_facebook/";
options.picture = "http://facebook.psyked.co.uk/packt/images/gold.png";
Facebook.api("me/feed", responseHandler, options, "post");
private function responseHandler(success:Object, fail:Object):void {
if (success) {
trace(JSON.encode(success, true));
} else {
trace(JSON.encode(fail, true));
}
}
35. Posting image data - FileReference
var options:Object = {};
options.fileName = fr.name;
options.image = fr.data;
var fileExtension:String = fr.name.split(".")[fr.name.split(".").length-1];
switch (fileExtension) {
case "gif":
options.contentType = "image/gif";
break;
case "png":
options.contentType = "image/png";
break;
case "jpeg":
case "jpg":
options.contentType = "image/jpeg";
break;
}
options.message = "Image caption";
Facebook.api("me/photos", photoUploadComplete, options, "post");
36. Editing existing Objects
• Editing objects (that can be edited) is done by
var options:Object = new Object();
making a POST request to an Objects’ URL.
options.name = "I found a trophy";
options.caption = "Gold trophy";
options.description = "I found a Gold trophy while using ...";
options.link = "http://apps.facebook.com/packt_facebook/";
options.picture = "http://facebook.psyked.co.uk/packt/images/gold.png";
Application, Album, Event, Group and User
Facebook.api("me/feed", responseHandler, options, "post");
objects have properties that can be edited.
private function responseHandler(success:Object, fail:Object):void {
if (success) {
trace(JSON.encode(success, true));
} else {
trace(JSON.encode(fail, true));
}
}
37. Deleting existing Objects
var options:Object = new Object();
options.method = "delete";
Facebook.api("10150322337577749", responseHandler, options,
"post");
private function responseHandler(success:Object,
fail:Object):void {
if (success) {
trace(JSON.encode(success, true));
} else {
trace(JSON.encode(fail, true));
}
}
Facebook is...For developers, it gives us (users, connections, possible invites)Find the SDK download link. Where did the SDK come from?Desktop & Web versions of the SDK. Both similar, different logins, I’m going to focus on the Flash Player version.Log into Facebook.com/developers and demo the interface.What is a Facebook application? (Canvas, non-canvas)The Graph API is one of the (the main) API for applications to get data from FB.
So, requesting Permissions is as simple as adding their code name in the options of a login request.Put in all the permissions, and Facebook will handle the process of working out what you have, and what you need.However, there’s a downside to requesting permissions, and that’s because Facebook makes it explicitly clear what permissions your app is requesting, and what your application can do with them. Too many permissions = turn off.Golden rule is, “if you don’t need those permissions immediately, don’t ask for them”.
Although you should be handling errors correctly, preventative measures and checking before performing actions are always better.Retrieving details about Extended Permissions can’t be done strictly with the Graph API, but we can retrieve this information through FQL (Facebook Query Language). FQL is an SQL-like syntax for querying Facebook.We log in with no permissions (or as little as we can get away with), and then query FQL to get a Dictionary object of permissions. Before you make a request that requires permissions, you check the object, and if you need those permissions, call the login request again, this time with a permissions object.
The API is REST-based, but we can’t make anything other than GET or POST requests in Flash (or JavaScript).Instead, we can supply a method parameter, with a value of delete.The request URL is the unique Facebook Id of the object you’re deleting.