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:
Reference for permissions: https://developers.facebook.com/docs/facebook-login/permissions/
Some fields can be specified wrong and that is why
Facebook API
is giving such an error even when everything else seems fine. For instanceprofile_pic
field can cause such an error which should be something likepicture.type(large)
. See also: FacebookGraphAPIError: (#210) This call requires a Page access token