Problem with get request "me"

Hello everyone!

I am having some trouble using the get request “me”. I am able to get all the data from the player without any problem using the facebook listener. My problem is how do I know when my request is done so I can load those player information to the screen. I tried to use the “event.didComplete”, but it always return “false”. I need something that could tell me when the request or even the login is done. Is there any “onComplete” method that I could create?

Thank you!

André Cândido.

Hi Kozz.  Can you post a snippet of the code you’re using and a link(s) to the docs you’ve referenced.  

I ask because ‘Facebook’ is a feature that has changed a lot.  I (and others probably) need a bit more context (insight into the exact implementation and assumptions you’re working from) to answer properly.

Thanks,

Ed (aka The Roaming Gamer)

Thanks for the quick response.

Here is a simple example that I’m using to get the player name and id from facebook and put into a player table. With this code, right after que player login into facebook, it requests his name and id. The base from this code I get from this doc: https://docs.coronalabs.com/daily/guide/social/implementFacebook/index.html

The first problem is that this doc says that you can only have one facebook listener that you inform when you do the login. But, the way I implemented, I think its not a good practice, I mean, I should call a function to login first, and then when its done, I call the other function to request my info, and then when its done I return the values to my game so I can show them on screen. The problem is I can’t know when the login or the request is done. I think it should have something like the onComplete method. So the last thing I tried was to use the “event.didComplete”, in the docs it says that it returns true when its done and false when not. https://docs.coronalabs.com/daily/api/event/fbconnect/didComplete.html

But I could not make it work, it always returns me false. 

Thank you again for the help.

local facebook = require( "facebook" ) local json = require( "json" ) local player = {} local function facebookListener( event ) print( "event.name:" .. event.name ) --"fbconnect" print( "isError: " .. tostring( event.isError ) ) print( "didComplete: " .. tostring( event.didComplete ) ) print( "event.type:" .. event.type ) --"session", "request", or "dialog" if ( "session" == event.type ) then --options are "login", "loginFailed", "loginCancelled", or "logout" if ( "login" == event.phase ) then local access\_token = event.token --code for tasks following a successful login local params = { fields = "id,first\_name,last\_name" } facebook.request( "me", "GET", params ) end elseif ( "request" == event.type ) then print("facebook request") if ( not event.isError ) then local response = json.decode( event.response ) player.id = response.id player.name = response.first\_name .. " " .. response.last\_name end elseif ( "dialog" == event.type ) then print( "dialog", event.response ) --handle dialog results here end end local fbAppID = "000000000000" --replace with your Facebook App ID facebook.login( fbAppID, facebookListener, { "user\_friends", "email" } )

You are printing out a lot of information.  What are you seeing in your console log?

http://docs.coronalabs.com/guide/basics/debugging/index.html

Rob

FWIW, I ran the above code, just putting in a table dumper to see everything in the event table.    It worked like a charm.  It fetched my first, last names and ID which is all you’re asking it to do.

If it’s not working for you, add in some more prints and watch your device’s console log.  I’m also on IOS if that matters.  If you’re testing on Android, the keyhash’s can be problematic in setting up the Facebook developer portal.  If you filtering out all non-Corona messages using “adb logcat Corona:v *:s” then just use “adb logcat” so that you can see what’s happening with the Facebook intent.

Rob

Thanks for answering Rob.  I got stuck doing other stuff and didn’t get back to this fast enough.

Thanks for the answer.

You said this code worked for you. And yes, this code works for me also, the problem is I don’t know when it’s done. The event.didComplete always return me false. What I am asking is for something that the fabebook listener could return to tell me when the request is done so I can do more requests or display the data I get on Facebook on my game. Hope you understand what I am trying to do.

Thanks for your attention!

The request is done when you get the event in the call back.  After you call facebook.request() you get an event with the following in the tableView

May 14 19:48:29 Rob-Miracles-iPhone fbtest[8969] <Warning>: table: 0x17027c040 {
May 14 19:48:29 Rob-Miracles-iPhone fbtest[8969] <Warning>:   [isError] => false
May 14 19:48:29 Rob-Miracles-iPhone fbtest[8969] <Warning>:   [type] => “request”
May 14 19:48:29 Rob-Miracles-iPhone fbtest[8969] <Warning>:   [name] => “fbconnect”
May 14 19:48:29 Rob-Miracles-iPhone fbtest[8969] <Warning>:   [response] => “{“id”:“XXXXXXXXXX”,“first_name”:“Rob”,“last_name”:“Miracle”}”
May 14 19:48:29 Rob-Miracles-iPhone fbtest[8969] <Warning>: }

You don’t get a set “didComplete” for facebook.request().  The .didComplete flag is for facebook.showDialog().  It will be true if the user committed the dialog’s action and false if they cancelled the dialog.   For facebook.request, you will get an event of type “request”.  If it succeeded then .isError will be false.  If it failed, .isError will be true.  Thus:

elseif ( "request" == event.type ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("facebook request") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( not event.isError ) then -- if you get here it was successful. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local response = json.decode( event.response ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; player.id = response.id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; player.name = response.first\_name .. " " .. response.last\_name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end

Look at where I posted the comment above – if you get here it was successful.  Then you were.

Rob

Thank you so much for your explanation. So what I was already doing was right haha. I just though that there was another way to see if some request was done.

Thank you!

André Cândido.

Hi Kozz.  Can you post a snippet of the code you’re using and a link(s) to the docs you’ve referenced.  

I ask because ‘Facebook’ is a feature that has changed a lot.  I (and others probably) need a bit more context (insight into the exact implementation and assumptions you’re working from) to answer properly.

Thanks,

Ed (aka The Roaming Gamer)

Thanks for the quick response.

Here is a simple example that I’m using to get the player name and id from facebook and put into a player table. With this code, right after que player login into facebook, it requests his name and id. The base from this code I get from this doc: https://docs.coronalabs.com/daily/guide/social/implementFacebook/index.html

The first problem is that this doc says that you can only have one facebook listener that you inform when you do the login. But, the way I implemented, I think its not a good practice, I mean, I should call a function to login first, and then when its done, I call the other function to request my info, and then when its done I return the values to my game so I can show them on screen. The problem is I can’t know when the login or the request is done. I think it should have something like the onComplete method. So the last thing I tried was to use the “event.didComplete”, in the docs it says that it returns true when its done and false when not. https://docs.coronalabs.com/daily/api/event/fbconnect/didComplete.html

But I could not make it work, it always returns me false. 

Thank you again for the help.

local facebook = require( "facebook" ) local json = require( "json" ) local player = {} local function facebookListener( event ) print( "event.name:" .. event.name ) --"fbconnect" print( "isError: " .. tostring( event.isError ) ) print( "didComplete: " .. tostring( event.didComplete ) ) print( "event.type:" .. event.type ) --"session", "request", or "dialog" if ( "session" == event.type ) then --options are "login", "loginFailed", "loginCancelled", or "logout" if ( "login" == event.phase ) then local access\_token = event.token --code for tasks following a successful login local params = { fields = "id,first\_name,last\_name" } facebook.request( "me", "GET", params ) end elseif ( "request" == event.type ) then print("facebook request") if ( not event.isError ) then local response = json.decode( event.response ) player.id = response.id player.name = response.first\_name .. " " .. response.last\_name end elseif ( "dialog" == event.type ) then print( "dialog", event.response ) --handle dialog results here end end local fbAppID = "000000000000" --replace with your Facebook App ID facebook.login( fbAppID, facebookListener, { "user\_friends", "email" } )

You are printing out a lot of information.  What are you seeing in your console log?

http://docs.coronalabs.com/guide/basics/debugging/index.html

Rob

FWIW, I ran the above code, just putting in a table dumper to see everything in the event table.    It worked like a charm.  It fetched my first, last names and ID which is all you’re asking it to do.

If it’s not working for you, add in some more prints and watch your device’s console log.  I’m also on IOS if that matters.  If you’re testing on Android, the keyhash’s can be problematic in setting up the Facebook developer portal.  If you filtering out all non-Corona messages using “adb logcat Corona:v *:s” then just use “adb logcat” so that you can see what’s happening with the Facebook intent.

Rob

Thanks for answering Rob.  I got stuck doing other stuff and didn’t get back to this fast enough.

Thanks for the answer.

You said this code worked for you. And yes, this code works for me also, the problem is I don’t know when it’s done. The event.didComplete always return me false. What I am asking is for something that the fabebook listener could return to tell me when the request is done so I can do more requests or display the data I get on Facebook on my game. Hope you understand what I am trying to do.

Thanks for your attention!

The request is done when you get the event in the call back.  After you call facebook.request() you get an event with the following in the tableView

May 14 19:48:29 Rob-Miracles-iPhone fbtest[8969] <Warning>: table: 0x17027c040 {
May 14 19:48:29 Rob-Miracles-iPhone fbtest[8969] <Warning>:   [isError] => false
May 14 19:48:29 Rob-Miracles-iPhone fbtest[8969] <Warning>:   [type] => “request”
May 14 19:48:29 Rob-Miracles-iPhone fbtest[8969] <Warning>:   [name] => “fbconnect”
May 14 19:48:29 Rob-Miracles-iPhone fbtest[8969] <Warning>:   [response] => “{“id”:“XXXXXXXXXX”,“first_name”:“Rob”,“last_name”:“Miracle”}”
May 14 19:48:29 Rob-Miracles-iPhone fbtest[8969] <Warning>: }

You don’t get a set “didComplete” for facebook.request().  The .didComplete flag is for facebook.showDialog().  It will be true if the user committed the dialog’s action and false if they cancelled the dialog.   For facebook.request, you will get an event of type “request”.  If it succeeded then .isError will be false.  If it failed, .isError will be true.  Thus:

elseif ( "request" == event.type ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("facebook request") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( not event.isError ) then -- if you get here it was successful. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local response = json.decode( event.response ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; player.id = response.id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; player.name = response.first\_name .. " " .. response.last\_name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end

Look at where I posted the comment above – if you get here it was successful.  Then you were.

Rob

Thank you so much for your explanation. So what I was already doing was right haha. I just though that there was another way to see if some request was done.

Thank you!

André Cândido.