API Teaser: Graph requests in Facebook

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0

Following our teaser from earlier today, we wanted to give you a preview/glimpse into how easy it soon will be to integrate Facebook into your app.

Behind the scenes, we’re integrating Facebook’s OAuth-based library for FBConnect support. It’s going to be insanely easy to access Facebook’s graph API.

Here’s the breakdown:

It’s a one-liner to prompt the user for login. The first argument is the app id that you get from Facebook and the second is a listener that responds to “fbconnect” events:
[cc lang=”lua”]
facebook.login( “1234567890”, listener )[/cc]

Once the user has logged in, it’s one line to get that user’s friends list:
[cc lang=”lua”]
facebook.request( “me/friends” )[/cc]

And one more line to convert the JSON response into a Lua table:
[cc lang=”lua”]
response = json.decode( event.response )[/cc]

Most of the rest of the code is for listener management and creating the scrolling list of friends โ€” I’ve blurred the names to protect the innocent. (BTW, the scrolling list is part of our cross-device UI framework. There’s a quick tutorial here).

Here’s the entire code:

[cc lang=”lua”]
local facebook = require “facebook”
local json = require “json”
local tableView = require(“tableView”)

— listener for “fbconnect” events
local function listener( event )
if ( “session” == event.type ) then
— upon successful login, request list of friends
if ( “login” == event.phase ) then
facebook.request( “me/friends” )
elseif ( “request” == event.type ) then
— event.response is a JSON object from the FB server
local response = event.response

— if request succeeds, create a scrolling list of friend names
if ( not event.isError ) then
response = json.decode( event.response )

local friends = {}
local data = response.data
for i=1,#data do
local name = data[i].name
table.insert( friends, name )

local topBoundary = display.screenOriginY + 40
local bottomBoundary = display.screenOriginY + 0

— create the list of items
myList = tableView.newList{
default=”listItemBg.png”, over=”listItemBg_over.png”,
top=topBoundary, bottom=bottomBoundary,
elseif ( “dialog” == event.type ) then
print( “dialog”, event.response )

— first argument is the app id that you get from Facebook
facebook.login( “1234567890”, listener )

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0

This entry has 12 replies

  1. Wow I was looking for this exact thing a while back, and ended up creating my own app-server-app code, but it requires an external web server.

    Thanks for this advice! In the future it’ll allow me to make graph requests straight from the app without the need for an external web server.

  2. Jordan says:

    REALLY cool!

  3. Edward says:

    Thanks and GREAT!

  4. Matus says:


  5. Horace Ho says:

    Wonderful! Thank you~

  6. Charlie says:

    This is what I love about having switched over from Titanium to Corona – much more openness on what’s coming up, simpler APIs – which actually work – and what so far seems like a much faster pace of development. Keep up the good work!

    Looking forward to getting this up and running soon. Maps API would be nice too ๐Ÿ˜‰

  7. That’s great! Looking forward for it!

  8. Wooooot!

  9. edgar says:

    Support for Facebook on Android soon?

  10. Jeff says:

    Please provide support for the Android platform. Release for iOS was over 3 months ago. This makes little sense considering one of Corona’s selling points is to simplify cross-platform development. I will not be investing in your software until this and similar problems are corrected.

  11. Richard says:

    Great example but do you know how I can pull the name out of the list for use? I’ve tried data[id].name and a few others but I can’t get the listener to recognize what I’m clicking on in order to display it even in a simple alert.