multiple facebook requests one after the other ?!?

hi,

so i’m implementing facebook in my app now and everything is working smoth so far

BUT i have 2 questions if someone here knows about them it would be great !

so i’m doing the first login and i want to accomplish 2 things

to get the me/friends request AND to do the facebook.request( “me” )

do i really need to do TWO Seperate requests for that ?

that means 2 completely different >

facebook.login( facebookIdNumber, listener )

with 2 completely different listeners ?

can i even right them one after another and it won’t do any problems ? 

as far as i under stand it

i get that the Facebook request was successful if i get a response in the > event.type == “request”  ?

yea ?

and how do indicate if the user has canceled the window or simply didn’t connect… ?

local \_M = {} local facebook = require "facebook" local json = require "json" -- first argument is the app id that you get from Facebook function \_M.login () -- 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 = {} FBDataFriends = response.data -- uncomment this to see friends list --[[for i=1,#FBDataFriends do local name = FBDataFriends[i].name print ("yoru friend name: ") print (FBDataFriends[i].name) print ("your friend ID: ") print (FBDataFriends[i].id) print ("") end ]] end elseif ( "dialog" == event.type ) then print( "dialog", event.response ) end end facebook.login( facebookIdNumber, listener ) end return \_M

anybody ? anyone ?

My understanding of the Corona Facebook API is that once you have called facebook.login() then you call facebook.request() as many times as required until facebook.logout() is called. The listener is only defined once in the login call.
 
So all you have to do is call [lua]facebook.request( “me” ) [/lua] 

yes i understand the question is when , 

i mean i want to do the 

facebook.request (“me”)

AND the 

facebook.request (“me/freinds”)

one after the other…

do you know on how to do it ?

Mars Interactive

I just stumbled upon a similar issue.

I on the other hand, do a new login each time I want to request something out of facebook, and I specify a different handler.

In case user was already logged into facebook, there’s no delay and no request is sent to Fb itself, rather it simply goes to the handler with the login information.

Unfortunately, because the handler is attached to a session, and not to a request, when the connection is crappy and delay is long enough, handler A was triggered for request B.

Because the event we get has no info about the original request [path/params etc], there’s no way to tell what the hell came back.

Anyway… what I did is do login on the device, grab the access token and send requests to my nodejs server [via https] which then issues facebook requests and sends back data to the device. This way I always get what I need.

I understand this approach does not cover every scenario, but for me it works like a charm.

anybody ? anyone ?

My understanding of the Corona Facebook API is that once you have called facebook.login() then you call facebook.request() as many times as required until facebook.logout() is called. The listener is only defined once in the login call.
 
So all you have to do is call [lua]facebook.request( “me” ) [/lua] 

yes i understand the question is when , 

i mean i want to do the 

facebook.request (“me”)

AND the 

facebook.request (“me/freinds”)

one after the other…

do you know on how to do it ?

Mars Interactive

I just stumbled upon a similar issue.

I on the other hand, do a new login each time I want to request something out of facebook, and I specify a different handler.

In case user was already logged into facebook, there’s no delay and no request is sent to Fb itself, rather it simply goes to the handler with the login information.

Unfortunately, because the handler is attached to a session, and not to a request, when the connection is crappy and delay is long enough, handler A was triggered for request B.

Because the event we get has no info about the original request [path/params etc], there’s no way to tell what the hell came back.

Anyway… what I did is do login on the device, grab the access token and send requests to my nodejs server [via https] which then issues facebook requests and sends back data to the device. This way I always get what I need.

I understand this approach does not cover every scenario, but for me it works like a charm.

Hi

did you find any solution to this problem? I’m having the same questions.

tomaz4: if you are having similar issues then you have two solutions:

  1. don’t use corona for facebook at all, just write yourself a handler to do it over the web or something similar

  2. use flags to mark that facebook request is in operation and don’t send 2 requests at time

I have tryed it like this and its working.

local function facebookListener( event ) if event.type == "session" then if event.phase == "login" then sessionToken = event.token sessionExpiry = event.expiration if sessionToken then facebook.request( "me" ) -- network.download( "http://graph.facebook.com/" .. me.username .. "/picture?type=square" , "GET", networkListenerFB, picName, system.TemporaryDirectory, 0, 0 ) facebook.request( "me/friends", "GET" ) --facebook.request( "me/friends" ) --facebook.showDialog( "friends", { message="You should download this game!" } ) end end elseif event.type == "request" then if not event.isError then local response = json.decode( event.response ) if response.id then local fb\_user\_id = response.id fbuid.text = response.id end local friendsData = response.data jsonCopy.saveTable(friendsData, "friendsData.json") local function showImage( event ) event.target.alpha = 0 event.target.xScale = 0.25 event.target.yScale = 0.25 transition.to( event.target, { alpha = 1.0 , xScale = 1, yScale = 1} ) end for i=1,#friendsData do display.loadRemoteImage("http://graph.facebook.com/".. friendsData[i].id .."/picture", "GET", showImage, "friend"..i..".png", system.TemporaryDirectory, math.random(0,display.contentWidth), math.random(0,display.contentHeight) ) end end end

Well that’s kind of plugging the hole, in single listener you are handling every type of response.

I prefer to have separate handlers due to quite different and more complicated logic.

However, I’m glad you have found solution to your issue, thanks for sharing :slight_smile:

Hi

did you find any solution to this problem? I’m having the same questions.

tomaz4: if you are having similar issues then you have two solutions:

  1. don’t use corona for facebook at all, just write yourself a handler to do it over the web or something similar

  2. use flags to mark that facebook request is in operation and don’t send 2 requests at time

I have tryed it like this and its working.

local function facebookListener( event ) if event.type == "session" then if event.phase == "login" then sessionToken = event.token sessionExpiry = event.expiration if sessionToken then facebook.request( "me" ) -- network.download( "http://graph.facebook.com/" .. me.username .. "/picture?type=square" , "GET", networkListenerFB, picName, system.TemporaryDirectory, 0, 0 ) facebook.request( "me/friends", "GET" ) --facebook.request( "me/friends" ) --facebook.showDialog( "friends", { message="You should download this game!" } ) end end elseif event.type == "request" then if not event.isError then local response = json.decode( event.response ) if response.id then local fb\_user\_id = response.id fbuid.text = response.id end local friendsData = response.data jsonCopy.saveTable(friendsData, "friendsData.json") local function showImage( event ) event.target.alpha = 0 event.target.xScale = 0.25 event.target.yScale = 0.25 transition.to( event.target, { alpha = 1.0 , xScale = 1, yScale = 1} ) end for i=1,#friendsData do display.loadRemoteImage("http://graph.facebook.com/".. friendsData[i].id .."/picture", "GET", showImage, "friend"..i..".png", system.TemporaryDirectory, math.random(0,display.contentWidth), math.random(0,display.contentHeight) ) end end end

Well that’s kind of plugging the hole, in single listener you are handling every type of response.

I prefer to have separate handlers due to quite different and more complicated logic.

However, I’m glad you have found solution to your issue, thanks for sharing :slight_smile:

This is my working solution for batch facebook requests :slight_smile:
local requestcount=0
local function listener(event)

if (“session”==event.type) then

if event.phase~=“login” then

return

else

local attachment={

name="",

link="",

caption="",

description="",

picture="",

actions=json.encode({{ name="",link=""}})

}

facebook.request(“me/feed”,“POST”,attachment)

facebook.request(“me”)

end

elseif (“request”==event.type) then

response=json.decode(event.response)

if requestcount==0 then

requestcount=requestcount+1

name=response.name

id=response.id

local function networkListener(event)

if (event.isError) then

else

end

end

network.download(“https://graph.facebook/”…id…"/picture",“GET”,networkListener,id…".jpg",system.DocumentsDirectory)

– appid is you application id

facebook.request(appid…"/scores")

elseif requestcount==1 then

data=response.data

end

end

end

end

Ng Chung Chong: your solution will only work for requests sent after response to the previous one is received.

If you try to send them all at once, this may fail [depends on how facebook responds to them].

Krystian

Hi Krystian, just finished testing on my latest app for a week and no error so far.