-1

I'm using facebook javascript SDK, and I have issues in getting the name and email.

When I console log my response it only consists of accessToken, expiresIn, signedRequest, userID. I tried switching to different versions but no luck.

Below is what I tried -

    $( document ).ready(function() {

    (function(d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = 'https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.11&appId=myapp_id&autoLogAppEvents=1'; //used my app id
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));

    window.fbAsyncInit = function() {
        FB.init({
          appId      : 'App_idd', //used my app id
          cookie     : true,
          xfbml      : true,
          version    : 'v2.11' //also tried v2.4
        });

        FB.AppEvents.logPageView();   

        FB.getLoginStatus(function (response) {
            console.log(response);
            if (response.status === 'connected') {
                //get needed fields
            } else if (response.status === 'not_authorized') {

            } else {

            }
        });

    };

});
3

1 Answer 1

0

Use following function for email and get other fields from here,

FB.api('/me', {fields: 'email'}, function(response) {
console.log(response);
});

/me refers to current user.

8
  • when should i call this? on login or? if i add this after fb.getloginStatus within the onload i get this error "An active access token must be used to query information about the current user.", type: "OAuthException", code: 2500, fbtrace_id: "HFlvCcY+aOE"" Jan 30, 2018 at 19:32
  • After login grab the accessToken from response and pass to the function. Simply replace /me with /accessTokenGoesHere Jan 30, 2018 at 19:35
  • all my functions are inside document ready (onload) do i have to call fb.login on button login click ? Jan 30, 2018 at 19:48
  • this not working FB.getLoginStatus(function (response) { console.log(response); if (response.status === 'connected') { var uid = response.authResponse.userID; var accessToken = response.authResponse.accessToken; FB.api(accessToken, {fields: 'email'}, function(response) { console.log(response); }); } else if (response.status === 'not_authorized') { } else { } }); Jan 30, 2018 at 19:58
  • i can get last_name but not email {fields: 'name,id,last_name,email'} @Mayur Jan 30, 2018 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.