Facebook Request Friends and Email

Hi,

Hopefully a fairly simple question - I want to do a single Facebook request to get both a user’s email address and list of friends.  So far I’ve managed to implement both through separate facebook logins, but I’m not sure how to do it through one login.

Here’s my code for the email login:

[lua]local function fbListener( event )

    if event.type == “session” then

      print(“session began”)

      if event.phase == “login” then

      myGameSettings.fbSessionToken = event.token

      saveTable(myGameSettings, “myGameSettings.json”)

          if myGameSettings.fbSessionToken then

               print(“got FB session token”)

               facebook.request( “me” )

          end

       end

    elseif event.type == “request” then

      if not event.isError then

        local response = json.decode( event.response )

        if response.email then

          myGameSettings.email = response.email

          saveTable(myGameSettings, “myGameSettings.json”)

        end

      end

    end

  end

facebook.login( fbAppID, fbListener,{ “email” } )[/lua]

My code for retrieving the list of friends is very similar, except that I use a second login function, [lua]facebook.login( fbAppID, fbListener2)[/lua] that calls [lua] facebook.request( “me/friends” ) [/lua] during the login event phase.

With the facebook API you really just need to login once.  Then you do facebook.request() calls for each thing you want back from facebook.   Because you only call login once, you only have one callback function.  In other words you can make many requests, but they all call the same function with an event.type of “request”.  You can look at the Facebook sample app to see how to handle multiple requests in a single handler function.

Rob

Hi Rob,

Thanks for the reply.  I’ve had a look at the Facebook sample app, and unless I’m missing something, it seems as though they’re calling one facebook.login() for every facebook.request() (they’re just using fbCommand to fire the appropriate facebook.request() in the facebook listener function):

[lua]    local function postMsg_onRelease( event )

        – call the login method of the FB session object, passing in a handler

        – to be called upon successful login.

        fbCommand = POST_MSG

        facebook.login( appId, listener, {“publish_actions”} )

    end

    

    local function showDialog_onRelease( event )

        – call the login method of the FB session object, passing in a handler

        – to be called upon successful login.

        fbCommand = SHOW_DIALOG

        facebook.login( appId, listener, {“publish_actions”}  )

    end[/lua]

The issue I’m having is that I want to request both the friends list and the email address with the click of one button in my Corona app.  At the moment I’m calling 2 login functions - for facebook.request( “me/friends” ) and facebook.request( “me” ).

The problem with this is that the second time I call facebook.login(), a Facbook login web view pop-up appears uneccessarily.  Please could you provide an example of how you would request the user’s email address and friends list with just one login function?

Thanks again,

Ed

Hi Rob,

Any updates?  I’m a little stuck at the moment…

Ed

Hi edmoyse.

Were you able to simply call

facebook.login( fbAppID, fbListener,{ "email" } )

just 1 time and receives the email? 

Here I am having to first call facebook.login without the { “email” } and then call facebook.login again with the { “email” }.

Hey Renato,

Yes - mine seems to work fine if I only want to get the email.  I’m using Version 2013.1202 (2013.8.28) [haven’t yet upgraded to graphics 2.0] if that makes any difference… 

If you post your fbListener function, I’ll happily compare it with mine just to double-check.

Ed

Also, Rob/anybody else,

I’d be grateful if you could help me find a solution to my original problem (having to call multiple login functions for multiple Facebook requests)… I still haven’t managed to solve this.

Best,

Ed

Actually I tried your code (and also Corona example), and the first facebook.login call it ignore any permission that I am passing.

About your problem, just look at the Corona example. Yes, in the example it calls facebook.login but there is no problem with that. That way you ensure that if the user is not logged in, the facebook will ask the user for login

I just executed these two blocks of code:

– in the facebook listener function after a successful login:

            local params = {
                fields = “name,username,first_name,last_name,picture,installed,email”
            }
            facebook.request( “me”, “GET”, params )

– and the login call

facebook.login( appId, listener, {“publish_actions”,“email”}  )

I used the Facebook sample app, added email permission and changed the GET_USER request to ask for the email and it worked as expected with just the one call to Facebook on iOS 7.

Rob

Thanks Rob. I will try that. The strange for me is that even calling 

facebook.login( appId, listener, {"publish\_actions","email"} )

the Facebook app shows me that my app is only requiring the public permissions, it does not show the email or publish, and then the request(me) doesn’t return the email.

I will try your solution and write here the outcome. 

With the facebook API you really just need to login once.  Then you do facebook.request() calls for each thing you want back from facebook.   Because you only call login once, you only have one callback function.  In other words you can make many requests, but they all call the same function with an event.type of “request”.  You can look at the Facebook sample app to see how to handle multiple requests in a single handler function.

Rob

Great - thanks Rob!  I’ll give it a go

Hi Rob,

Thanks for the reply.  I’ve had a look at the Facebook sample app, and unless I’m missing something, it seems as though they’re calling one facebook.login() for every facebook.request() (they’re just using fbCommand to fire the appropriate facebook.request() in the facebook listener function):

[lua]    local function postMsg_onRelease( event )

        – call the login method of the FB session object, passing in a handler

        – to be called upon successful login.

        fbCommand = POST_MSG

        facebook.login( appId, listener, {“publish_actions”} )

    end

    

    local function showDialog_onRelease( event )

        – call the login method of the FB session object, passing in a handler

        – to be called upon successful login.

        fbCommand = SHOW_DIALOG

        facebook.login( appId, listener, {“publish_actions”}  )

    end[/lua]

The issue I’m having is that I want to request both the friends list and the email address with the click of one button in my Corona app.  At the moment I’m calling 2 login functions - for facebook.request( “me/friends” ) and facebook.request( “me” ).

The problem with this is that the second time I call facebook.login(), a Facbook login web view pop-up appears uneccessarily.  Please could you provide an example of how you would request the user’s email address and friends list with just one login function?

Thanks again,

Ed

Hi Rob,

Any updates?  I’m a little stuck at the moment…

Ed

Hi edmoyse.

Were you able to simply call

facebook.login( fbAppID, fbListener,{ "email" } )

just 1 time and receives the email? 

Here I am having to first call facebook.login without the { “email” } and then call facebook.login again with the { “email” }.

Hey Renato,

Yes - mine seems to work fine if I only want to get the email.  I’m using Version 2013.1202 (2013.8.28) [haven’t yet upgraded to graphics 2.0] if that makes any difference… 

If you post your fbListener function, I’ll happily compare it with mine just to double-check.

Ed

Also, Rob/anybody else,

I’d be grateful if you could help me find a solution to my original problem (having to call multiple login functions for multiple Facebook requests)… I still haven’t managed to solve this.

Best,

Ed

Actually I tried your code (and also Corona example), and the first facebook.login call it ignore any permission that I am passing.

About your problem, just look at the Corona example. Yes, in the example it calls facebook.login but there is no problem with that. That way you ensure that if the user is not logged in, the facebook will ask the user for login

I just executed these two blocks of code:

– in the facebook listener function after a successful login:

            local params = {
                fields = “name,username,first_name,last_name,picture,installed,email”
            }
            facebook.request( “me”, “GET”, params )

– and the login call

facebook.login( appId, listener, {“publish_actions”,“email”}  )

I used the Facebook sample app, added email permission and changed the GET_USER request to ask for the email and it worked as expected with just the one call to Facebook on iOS 7.

Rob

Thanks Rob. I will try that. The strange for me is that even calling 

facebook.login( appId, listener, {"publish\_actions","email"} )

the Facebook app shows me that my app is only requiring the public permissions, it does not show the email or publish, and then the request(me) doesn’t return the email.

I will try your solution and write here the outcome.