I made this Facebook helper module which I think is pretty useful and more manageable method of doing things than the Facebook Sample app showcases. It could be more advanced I guess but trying to keep it simple.
Example usage:
[lua]
local Facebook = require(“FacebookHelper”)
local function getFriendsEvent(data)
if not data then print(“There was an error”) return end
print(“Friends Data”, data)
end
Facebook.friends(getFriendsEvent)
[/lua]
Bottom of module are the public functions available, only a couple of them, they are easy to add as needed.
Limitation: You can only have one Facebook listener AFAIK, thus you must wait for one request to finish before making a new one or things may get mixed up.
Module, FakebookHelper.lua:
[lua]
local appId = “1234567890” – Add your App ID here (also go into build.settings and replace XXXXXXXXX with your appId under CFBundleURLSchemes)
local facebook = require(“facebook”)
local json = require(“json”)
local M = {}
local logInCallback = nil
local requestCallback = nil
– Listener for all FB responses
local function listener(event)
local eventType = nil
if event and event.type then eventType = event.type end
if eventType == “session” then
if event.phase ~= “login” then – event.phase is one of: “login”, “loginFailed”, “loginCancelled”, “logout”
– Login failed
logInCallback(false)
return
end
logInCallback(true)
elseif eventType == “request” then
local response = json.decode( event.response )
if event.isError then
requestCallback(false)
print(“FB: Request failed!”)
else
requestCallback(response)
end
end
end --End listener function
local function logIn(callback)
logInCallback = callback
facebook.login(appId, listener, {“publish_actions”})
end
local function makeRequest(requestString, callback, httpMethod, params)
– Called when we get request data from FB
local function callBackRequest(data)
callback(data)
end
– Called when we get login response from FB
local function callbackLogin(successful)
if not successful then
print(“FB: Failed logging in”)
callback(false);
return
end
requestCallback = callBackRequest
facebook.request(requestString, httpMethod, params)
end
logIn(callbackLogin) – Login before every request
end
– Data example {“link”:“https://www.facebook.com/profile.php?id=100006500266597”,“id”:“100006500266597”,“name”:“Dorothy Amfekbffeig Rosenthalwitz”,“first_name”:“Dorothy”,“middle_name”:“Amfekbffeig”,“last_name”:“Rosenthalwitz”,“gender”:“female”,“timezone”:9,“locale”:“en_US”,“updated_time”:“2013-08-27T08:59:43+0000”}
function M.myInfo(callback)
makeRequest(“me”, callback)
end
function M.friends(callback)
makeRequest(“me/friends”, callback)
end
– Data example: {“data”: [{“id”: “100006585216282”, “name”: “Lisa Amfehebafbhb Letuchysen”,“installed”: true},{“id”: “100006655621519”,“name”: “Margaret Amffeefbaeai Warmansky”}], “paging”: {“next”: “https://graph.facebook.com/100006500266597/friends?fields=name,installed&format=json&access_token=CAAFz010EEZBIBAPMCtNbuicXlBjRjIBZBTMGzojEdVeJQbrHlL3y72JbUri05tSPkZBvzXfGjMQlnMjDIBbXGuGmw5lHG7M1M4te89SGHw0WNiVNt81iHYF5GpBt5urZBtR42rCytVPPjMIEQfZCXBZCIsQDqI7ENUktMoZAENC6IsTKuPm6bdm9Dl5XiXxrfKA8Bn4xzkAd7d8FZBP4dpp2&limit=5000&offset=5000&__after_id=100006655621519”}}
function M.installedFriends(callback)
makeRequest(“me/friends”, callback, “GET”, {fields = “name,installed”})
end
– Post text directly onto ones wall
function M.wallPost(text, callback)
makeRequest(“me/feed”, callback, “POST”, {message=text})
end
– Wallpost linking to a site, example: http://imgur.com/lKQNI4w
function M.wallPostSite(title, body, caption, link, callback)
local attachment = {
name = title,
link = link,
caption = caption,
description = body
}
makeRequest(“me/feed”, callback, “POST”, attachment)
end
– Untested
function M.postPhoto(name, link, caption, description, pictureUrl, actions)
local attachment = {
name = name, – “Developing a Facebook Connect app using the Corona SDK!”,
link = link, – “http://www.coronalabs.com/links/forum”,
caption = caption, – “Link caption”,
description = description, – “Corona SDK for developing iOS and Android apps with the same code base.”,
picture = pictureUrl, – “http://www.coronalabs.com/links/demo/Corona90x90.png”,
actions = json.encode(actions) – { { name = “Learn More”, link = “http://coronalabs.com” } }
}
makeRequest(“me/feed”, callback, “POST”, attachment)
end
return M
[/lua]
Edit: Added JSON decode to request response