Retrieving Users ID from a Facebook Friend List

Hi everyone !

I’m having a problem with getting a friends list from Facebook. Right now, I’m using the Facebook V4 api. I’d like to allow the user to connect to Facebook, retrieve his friends list, select one of them and get their user ID (or “app scoped user ID”).

So far, everythink seems to work : I’m able to log in to Facebook, to retrieve the friends list, show the dialog popup, select a friend who has installed the app. The problem is that the returned data is totally empty.

I do have some event.data, I’m also able to print #event.data, but when I try to print the firstName, the lastName, the firstName, or the id, there’s nothing.

-- Asking for permissions facebook.login(fbAppID, facebookListener, {"publish\_actions, user\_friends"}) -- Facebook Friends List Listener local function onCompleteFB(event) if event.data ~= nil then print("Number of selected friends : " .. #event.data) print("#event.data[1] : " .. #event.data[1]) end end -- Showing the friends list facebook.showDialog("friends", onCompleteFB)

print(#event.data)  returns a table but #event.data[1] returns 0 (instead of 2 in my case, since I’ve got 2 other friends using the app and who showed up in the dialog popup).

How can I retrieve the friends user ID ?

Just wanted to add that, since my Facebook App is pretty old (made in 2014), I decided to create a new app. Just in case since I’ve read that Graph 2.0 changed a lot of things…

Well, it didn’t change anything : I’m still able to do anything with Facebook, except retrieving infos (IDs, Name, etc) from my friends.

… help ! :wink:

I really don’t get it… 

I’ve tried a lot of things. I’m able to do everything with Facebook (showing the friends popup, choosing one or more friends) except one thing : the event.data is empty : I can’t print event.data[1].id or event.data[1].fullName.

Weirdest thing is that, the number of selected friends, #event.data, returns 1 if I’ve selected one friend, or returns 2 if I’ve selected two friends.

Has anyone managed to show the friends popup, then select a friend to finally get some data in return ?

I would suggest you use a table printing method and see what all is in even.data.  You could do:

print( json.prettify( event.data ) )

To dump your table quite easily. Look and see what all data you’re getting back.  event.data[1] should be your first friend and should be it’s own table of items.

Rob

Hey Rob !

Thanks for answering ! Well, I was using a the printTable function shown in the “Facebook Guide” to see all the data I was getting back  :

local function printTable( t, label, level ) if label then print( label ) end level = level or 1 if t then for k,v in pairs( t ) do local prefix = "" for i=1,level do prefix = prefix .. "\t" end print( prefix .. "[" .. tostring(k) .. "] = " .. tostring(v) ) if type( v ) == "table" then print( prefix .. "{" ) printTable( v, nil, level + 1 ) print( prefix .. "}" ) end end end end

By calling this function, it only returned something like that “event.data = table: 0x7fb1bbd00750” (don’t remember for sure, I’ve made a loooooot of tests). And then, printing #event.data returned the expected number of selected friends. But that was it.

After changing a lot of things, I eventually managed to get some data. I honestly don’t know why it finally worked, but it did.

Right now, I’m trying to clean my code and once I’m done, I’ll post it here. Maybe it will help someone, or maybe someone else will be able to see why it didn’t work at first.

… I must say : working with Facebook Graph is really challenging !

That method only dumps one level of tables. Frequently returned data will be tables inside of tables and using something like I suggested above using json.prettitfy or the print_r() function from the community code (print_r is the PHP table dumper).  Because facebook data is returning you a table of tables, it’s important to be able to see what’s in those sub-tables.

Rob

I think you might be right ! printTable() actually allows one “level parameter”. And at some point, I changed it to see how “deep” it could go, so I tried

printTable(response, "GET FRIENDS", 5)

instead of

printTable(response, "GET FRIENDS", 3)

And it might be the reasonI couldn’t see the full “hidden” content !

But since I’m not totally sure (because I’ve made a lot of tests, changing my Facebook Listener, etc), I’m gonna keep trying to clean my code and try to isolate the issue…

Just wanted to add that, since my Facebook App is pretty old (made in 2014), I decided to create a new app. Just in case since I’ve read that Graph 2.0 changed a lot of things…

Well, it didn’t change anything : I’m still able to do anything with Facebook, except retrieving infos (IDs, Name, etc) from my friends.

… help ! :wink:

I really don’t get it… 

I’ve tried a lot of things. I’m able to do everything with Facebook (showing the friends popup, choosing one or more friends) except one thing : the event.data is empty : I can’t print event.data[1].id or event.data[1].fullName.

Weirdest thing is that, the number of selected friends, #event.data, returns 1 if I’ve selected one friend, or returns 2 if I’ve selected two friends.

Has anyone managed to show the friends popup, then select a friend to finally get some data in return ?

I would suggest you use a table printing method and see what all is in even.data.  You could do:

print( json.prettify( event.data ) )

To dump your table quite easily. Look and see what all data you’re getting back.  event.data[1] should be your first friend and should be it’s own table of items.

Rob

Hey Rob !

Thanks for answering ! Well, I was using a the printTable function shown in the “Facebook Guide” to see all the data I was getting back  :

local function printTable( t, label, level ) if label then print( label ) end level = level or 1 if t then for k,v in pairs( t ) do local prefix = "" for i=1,level do prefix = prefix .. "\t" end print( prefix .. "[" .. tostring(k) .. "] = " .. tostring(v) ) if type( v ) == "table" then print( prefix .. "{" ) printTable( v, nil, level + 1 ) print( prefix .. "}" ) end end end end

By calling this function, it only returned something like that “event.data = table: 0x7fb1bbd00750” (don’t remember for sure, I’ve made a loooooot of tests). And then, printing #event.data returned the expected number of selected friends. But that was it.

After changing a lot of things, I eventually managed to get some data. I honestly don’t know why it finally worked, but it did.

Right now, I’m trying to clean my code and once I’m done, I’ll post it here. Maybe it will help someone, or maybe someone else will be able to see why it didn’t work at first.

… I must say : working with Facebook Graph is really challenging !

That method only dumps one level of tables. Frequently returned data will be tables inside of tables and using something like I suggested above using json.prettitfy or the print_r() function from the community code (print_r is the PHP table dumper).  Because facebook data is returning you a table of tables, it’s important to be able to see what’s in those sub-tables.

Rob

I think you might be right ! printTable() actually allows one “level parameter”. And at some point, I changed it to see how “deep” it could go, so I tried

printTable(response, "GET FRIENDS", 5)

instead of

printTable(response, "GET FRIENDS", 3)

And it might be the reasonI couldn’t see the full “hidden” content !

But since I’m not totally sure (because I’ve made a lot of tests, changing my Facebook Listener, etc), I’m gonna keep trying to clean my code and try to isolate the issue…