質問

I am trying to use the Facebook API that provides first_name, last_name, gender, email, birthday for signup on my website. I used the Graph API and generated an access token with necessary scope. I tried on my personal account in which I created the app and it returned the fields properly, but for FB test user and friend's account, it returns email and birthday as "undefined". In graph.facebook.com it shows the following error for a friend's account.

enter image description here

Facebook returns email id for my friend's same account on doulingo.

Am I setting the permissions incorrectly or is there any other problem ?

役に立ちましたか?

解決

The problem with my code was all because of not using access_token. I used access_token to retrieve all, first_name, last_name, gender, birthday, email.

function authUser() {
            FB.login(checkLoginStatus, {scope:'email, user_likes'});
            function checkLoginStatus(response) {
                if(!response || response.status !== 'connected') {
                    alert('User is not authorized');
                    document.getElementById('loginButton').style.display = 'block';
                } else {
                    alert('User is authorized');
                    document.getElementById('loginButton').style.display = 'none';
                    console.log('Access Token: ' + response.authResponse.accessToken);

                    //This has to be in your callback!
                    var uid = response.authResponse.userID;
                    var accessToken = response.authResponse.accessToken;
                // access token is very important to get some fields like email, birthday

                    getData();
                }
            }
        }


    function getData() {
        console.log('Welcome!  Fetching your information.... ');
        FB.api('/me', function(response) {
            console.log('Good to see you, ' + response.name + '.' + response.id);
        });
    };

他のヒント

check my answer here to see that is a well known issue but not a bug for facebook developers.

Editing.. This is what they say in the facebook developers site:

"...The documentation for the 'email' field of the 'user' object ( https://developers.facebook.com/docs/reference/api/user/ ) clarifies the expected behaviour here, which is: "this field will not be returned if no valid email address is available".

There are a number of circumstances in which you may think a user should have an email address returned but they will not. For privacy and security reasons it is not possible to elaborate on the precise reason any specific user's email address will not be returned so please do not ask. Some possible reasons:

No Email address on account; No confirmed email address on account; No verified email address on account; User entered a security checkpoint which required them to reconfirm their email address and they have not yet done so; Users's email address is unreachable.

You also need the 'email' extended permission, even for users who have a valid, confirmed, reachable email address on file..."

Click on the "Get Access Token" button and select permissions you need. It will regenerate the access token and email and other values will became available.

Note:

  • To access current user's birthday you need "user_birthday" permission
  • To access friend's birthdays you need "friends_birthday" permission
  • You cannot access friends email at any cost.
  • In account setting user needs to set his email as "Primary" in order to be accessed otherwise it will return null.

See this and this threads for more details.

//----------
//All you need do is add the fields you want as query strings as follows:
//----------


FB.api('/me?fields=first_name,email,last_name,name', function(response) {
  console.log('Good to see you, ' + response.name + '.' + response.first_name);
});

//----------

//I hope that helps!!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top