This call requires a Page access token

Viewed 3621

I am trying to login with facebook, which is done. I need to get user info like name, email, hometown, location, gender, profile_pic. But i can only get username of the logged in user. I also added fields parameter to api. You can see that in below code.

<div id="container"></div>
<script>
 var accessToken;
 window.fbAsyncInit = function() {
   FB.init({
    appId      : 'App Id',
    cookie     : true,
    xfbml      : true,
    version    : 'v3.1'
   });

   FB.AppEvents.logPageView();   

   function getuserProfile() {
    FB.api('/me?fields=id,name,email,birthday,gender,hometown,location,profile_pic', {access_token : accessToken }, function (response){
      console.log(response);
    });
 }  

FB.login(function(response) {
  if (response.status === 'connected') {
    accessToken = response.authResponse.accessToken;
    getuserProfile();
  } else {
    console.log('not connected');
  }
}, {scope: 'email'});  
};

(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";
  fjs.parentNode.insertBefore(js, fjs);
 }(document, 'script', 'facebook-jssdk'));

2 Answers

Make sure you get asked for the email permission in the login popup. Also, you need to ask for additional permissions with the scope parameter, not just email. For example, user_birthday to get the birthday of users. Right now you are only asking for the email permission.

Example:

FB.login(function(response) {
  if (response.status === 'connected') {
    accessToken = response.authResponse.accessToken;
    getuserProfile();
  } else {
    console.log('not connected');
  }
}, {scope: 'email,user_birthday,...'});  
};

Reference for permissions: https://developers.facebook.com/docs/facebook-login/permissions/

Related