API Teaser: Graph requests in Facebook

Posted by

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:

facebook.login( "1234567890", listener )

Once the user has logged in, it’s one line to get that user’s friends list:

facebook.request( "me/friends" )

And one more line to convert the JSON response into a Lua table:

response = json.decode( event.response )

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:

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" )
        end
    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 )
            end

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

            -- create the list of items
            myList = tableView.newList{
                data=friends,
                default="listItemBg.png", over="listItemBg_over.png",
                top=topBoundary, bottom=bottomBoundary,
            }
        end
    elseif ( "dialog" == event.type ) then
        print( "dialog", event.response )
    end
end

-- first argument is the app id that you get from Facebook
facebook.login( "1234567890", listener )

Ready to get started?

Create amazing games and apps for iOS & Android

12 Comments

Jonathan BeebeNovember 4th, 2010 at 7:11 pm

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.

JordanNovember 4th, 2010 at 7:25 pm

REALLY cool!

EdwardNovember 4th, 2010 at 8:05 pm

Thanks and GREAT!

MatusNovember 5th, 2010 at 12:26 am

Cool!

Horace HoNovember 5th, 2010 at 12:31 am

Wonderful! Thank you~

CharlieNovember 5th, 2010 at 2:57 am

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 ;)

Ricardo RauberNovember 5th, 2010 at 3:31 am

That’s great! Looking forward for it!

Radamanthus BatnagNovember 5th, 2010 at 8:27 am

Wooooot!

[...] what is it? [...]

edgarJanuary 12th, 2011 at 11:05 pm

Support for Facebook on Android soon?

JeffJanuary 23rd, 2011 at 5:26 pm

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.

RichardJanuary 24th, 2011 at 1:48 pm

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.

Leave a comment

Your comment