Posted on 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:
[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” )
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 )
[/cc]


Posted by . Thanks for reading...

12 Responses to “API Teaser: Graph requests in Facebook”

  1. Jonathan Beebe

    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.

    Reply
  2. Charlie

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

    Reply
  3. Jeff

    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.

    Reply
  4. Richard

    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.

    Reply

Leave a Reply

  • (Will Not Be Published)