1

I'm using the Facebook Javascript SDK to implement Facebook Login. I'm trying to grab the name, email, location, likes, friends, etc. and redirect the user to on-boarding.

I'm able to get accessToken and userID values;however, I'm trying to grab other user information I specifically put in my scope. I've tried to look at Facebook returns undefined for email and birthday and other questions but no luck

function fb_login(){
    FB.login(function(response) {
        if (response.authResponse) {
            console.log('Welcome!  Fetching your information.... ');
            console.log(response); // dump complete info
            var accessToken =   FB.getAuthResponse()['accessToken'];
            console.log('Access Token = '+ accessToken);
            var userID = FB.getAuthResponse()['userID']; //get FB UID
            console.log("userID: "+ userID);
            //refer to getData() function below
            getData();
        } else {
            //user hit cancel button
            console.log('User cancelled login or did not fully authorize.');
        }
    }, {scope: 'public_profile,email,user_friends,user_likes,user_location'});
}

function getData() {
  FB.api('/me', function(response) {
    console.log("email: "+response.email);
    // you can store this data into your database
    console.log('Good to see you, ' + response.name + '.');
      //check if the loginStatus works
      FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
          // the user is logged in and has authenticated your
          // app, and response.authResponse supplies
          // the user's ID, a valid access token, a signed
          // request, and the time the access token 
          // and signed request each expire

          //redirect to start/location.ejs
          //window.location = "start/location";

        } else if (response.status === 'not_authorized') {
          // the user is logged in to Facebook, 
          // but has not authenticated your app
        } else {
          // the user isn't logged in to Facebook.
        }
      });
  });
}

Why is this?

Community
  • 1
  • 1
Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81

1 Answers1

2

I was able to figure it out. Had to define some fields...now I have to figure out how to get a bigger profile pic from the user

function fb_login(){
    FB.login(function(response) {
        if (response.authResponse) {
            console.log('Welcome!  Fetching your information.... ');
            console.log(response); // dump complete info
            var accessToken =   FB.getAuthResponse()['accessToken'];
            console.log('Access Token = '+ accessToken);
            var userID = FB.getAuthResponse()['userID']; //get FB UID
            console.log("userID: "+ userID);
            //refer to getData() function below
            getData(accessToken);
        } else {
            //user hit cancel button
            console.log('User cancelled login or did not fully authorize.');
        }
    }, {'scope': 'public_profile,email,user_friends,user_likes,user_location'});
}

function getData(accessToken) {
  FB.api('/me', 'get', { access_token: accessToken, fields: 'id,name,gender,email,location,friends,likes,picture' }, function(response) {
    console.log(response);
});
  //check if the loginStatus works
  FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
      // the user is logged in and has authenticated your
      // app, and response.authResponse supplies
      // the user's ID, a valid access token, a signed
      // request, and the time the access token 
      // and signed request each expire

      //redirect to start/location.ejs
      //window.location = "start/location";

    } else if (response.status === 'not_authorized') {
      // the user is logged in to Facebook, 
      // but has not authenticated your app
    } else {
      // the user isn't logged in to Facebook.
    }
  });
}
Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81