Need help with order of execution for Facebook requests

Hey all!  I have properly connected my app to Facebook, but I am struggling with the best syntax and order of execution to do the following:

  1. prompt user to login if not logged in after scene loads

  2. get logged in user’s id

  3. get list of friends and save their ID’s in a table (I know this only shows users logged in to the app, right?)

  4. button that allows user to “invite” friends to the game via facebook popup

  5. button that allows user to “logout” or disconnect from facebook

I think I’m getting my login, request, etc. and listeners mixed up because things don’t seem to be working very well.  Any help or advice on how to most efficiently accomplish this?  I must be making it more difficult than it really is, so I’m sure we’d all appreciate a simple example to accomplish the above items

Thanks!!!

So, I REALLY need help here.

I guess I’m confused.

So, I get that I can setup the listener with the facebook.init call, so do I include the code to respond to my facebook.request statements there, too?

I guess I’m also confused by when to check facebook.isActive and if there is an active token - do I need to check that every time in the listener function?

How do I best structure my listener function to 1. check if user is logged in, 2. if not, do facebook.login  3. if so, get their id and their friends that have installed the game?

HELP!!

So, thoughts on this approach, which seems to work.  Am I overlooking anything?

local function facebookListener( event ) print( "Facebook initialized" ) if ( facebook.getCurrentAccessToken() == nil ) then print( "Login required!" ) facebook.login() else print( "Already logged in!" ) if "request"==event.type then local response = event.response local t=json.decode(event.response) print(response) if t.id then print(" MY ID="..t.id) facebook.request("me/friends") elseif t.data then print(" GET USER FRIENDS LIST") end else facebook.request( "me" ) end end end facebook.init( facebookListener )

That would seem to be a reasonable looking process.  The only issue is that calling facebook.request(“me/friends”) is going to generated another event.type of “request”. You just have to make sure that all future requests won’t return a .id member since it would trigger another friends request.

Rob

Ok. makes sense… Is there a way to identify which request was made in the listener instead?   Can I check for when I request “me” or “me/friends”  in the event table or something that will help me isolate the specific request made instead of just checking if id is returned?

Not really. You can set a flag to know what the last request was and just make sure to not make multiple requests at a time.

Rob

So, I REALLY need help here.

I guess I’m confused.

So, I get that I can setup the listener with the facebook.init call, so do I include the code to respond to my facebook.request statements there, too?

I guess I’m also confused by when to check facebook.isActive and if there is an active token - do I need to check that every time in the listener function?

How do I best structure my listener function to 1. check if user is logged in, 2. if not, do facebook.login  3. if so, get their id and their friends that have installed the game?

HELP!!

So, thoughts on this approach, which seems to work.  Am I overlooking anything?

local function facebookListener( event ) print( "Facebook initialized" ) if ( facebook.getCurrentAccessToken() == nil ) then print( "Login required!" ) facebook.login() else print( "Already logged in!" ) if "request"==event.type then local response = event.response local t=json.decode(event.response) print(response) if t.id then print(" MY ID="..t.id) facebook.request("me/friends") elseif t.data then print(" GET USER FRIENDS LIST") end else facebook.request( "me" ) end end end facebook.init( facebookListener )

That would seem to be a reasonable looking process.  The only issue is that calling facebook.request(“me/friends”) is going to generated another event.type of “request”. You just have to make sure that all future requests won’t return a .id member since it would trigger another friends request.

Rob

Ok. makes sense… Is there a way to identify which request was made in the listener instead?   Can I check for when I request “me” or “me/friends”  in the event table or something that will help me isolate the specific request made instead of just checking if id is returned?

Not really. You can set a flag to know what the last request was and just make sure to not make multiple requests at a time.

Rob