July 25, 2013

Cannot extract information like user's email address from facebook connect

Starx’s Question:

I am trying to use simple Facebook Connect to login users on a website. I am using most basic form of the code to do this:

FB.getLoginStatus(function(response) {
  if (response.status == 'connected') {
    getCurrentUserInfo(response)
  } else {
    FB.login(function(response) {
      if (response.authResponse){
        getCurrentUserInfo(response)
      } else {
        console.log('Auth cancelled.')
      }
    }, { scope: 'email' });

  }
});

function getCurrentUserInfo() {
  FB.api('/me', function(userInfo) {
    console.log(userInfo);
  });
};

I get the basic information only but I do not get any email. I have also set the permission settings on the application too.

Application permission Settings

I test the code using my facebook account. I can connect successfully but I cannot see my email address and my settings for my email is also public.

Can I get some help with this?

Ok, Just figured this out. The problem was that the scope was somehow not being passed to the login function.

All I had to do was modify the fbml code for the login button with the scope and it was asking for the email address authorisation.

<fb:login-button show-faces="true" width="200" max-rows="1" scope="email"></fb:login-button>

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!