Facebook JS SDK's FB.api('/me')方法不'不返回我在Grap

Facebook JS SDK's FB.api('/me') method doesn't return the fields I expect in Graph API v2.4+

本文关键字:方法 返回 Grap me SDK JS FB Facebook api      更新时间:2023-09-26

我试图使用Facebook api获得一些基本信息,但到目前为止,我只获得用户的名称和id。如{ name: "Juan Fuentes", id: "123456" }

我需要得到更多的信息,如电子邮件,名字,姓氏和生日

这是我的js代码

function facebookLogin() {
  FB.login(function(response) {
    var token = response.authResponse.accessToken;
    var uid = response.authResponse.userID;
    if (response.authResponse) {
      FB.api('/me', 'get', { access_token: token }, function(response) {
        console.log(response);
      });
      FB.api('/'+uid, 'get', { access_token: token }, function(response) {
        console.log(response);
      });
    }
  },
  { scope: 'public_profile' }
  );
}

这是激活它的按钮

<a id="fb-login" href="#" onclick="facebookLogin()"></a>

您需要手动指定每个字段,因为Graph API v2.4:

  • https://developers.facebook.com/docs/apps/changelog v2_4
  • https://developers.facebook.com/docs/javascript/reference/FB.api
<

声明字段/strong>
为了尝试提高移动网络上的性能,v2.4中的node和edge要求您显式地请求GET请求所需的字段。例如,GET/v2.4/me/feed默认不再包含喜欢和评论,但是GET/v2.4/me/feed?Fields =comments,likes将返回数据。要了解更多详细信息,请参阅如何请求特定字段的文档。

FB.api('/me', 'get', { access_token: token, fields: 'id,name,gender' }, function(response) {
    console.log(response);
});

对于来自public_profile作用域的数据也可以使用这种语法(在Graph API v2.9中进行了测试):

FB.api('/me?fields=birthday,link,gender,age_range', function(response) {
   console.log(response);
});

您可以在Graph API Explorer中在线测试可能的值,只需单击"Get Token"按钮:

https://developers.facebook.com/tools/explorer/?method=GET&路径=我% 3 ffields % 3 dbirthday % 2叮当声% 2 cgender % 2 cage_range& version = v2.9

完整的脚本如下:

jQuery(document).ready(function () {
    openLoginPopup();
})
function openLoginPopup() {
    FB.getLoginStatus(function (response) {
        if (response.status == 'connected') {
            getCurrentUserInfo(response);
        } else {
            FB.login(function (response) {
                if (response.authResponse) {
                    getCurrentUserInfo(response);
                } else {
                    console.log('Auth cancelled.');
                }
            }, {scope: 'email'});
        }
    });
}
function getCurrentUserInfo() {
    FB.api('/me?fields=id,email,first_name,last_name,name', function (userInfo) {
        console.log(userInfo.name + ': ' + userInfo.email);
    });
}
window.fbAsyncInit = function () {
    FB.init({
//        appId: 'xxxxxxxxxxxxxxxxxxxxx', //livemode
        appId: 'xxxxxxxxxxxx', //testmode
        cookie: true, // Enable cookies to allow the server to access the session.
        xfbml: true, // Parse social plugins on this webpage.
        version: 'v4.0'           // Use this Graph API version for this call.
    });
};

(function (d, s, id) {                      // Load the SDK asynchronously
    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'));

谢谢。

请注意,电子邮件并不总是通过调用email作为字段的me api返回,即使范围email被请求和授予,如果例如用户注册了一个电话号码:

https://developers.facebook.com/docs/facebook-login/permissions reference-email