Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Facebook JS SDK';s FB.api(';/me';)方法不';t返回我在Graph API v2.4+;_Javascript_Facebook - Fatal编程技术网

Javascript Facebook JS SDK';s FB.api(';/me';)方法不';t返回我在Graph API v2.4+;

Javascript Facebook JS SDK';s FB.api(';/me';)方法不';t返回我在Graph API v2.4+;,javascript,facebook,Javascript,Facebook,我试图使用Facebook api获取一些基本信息,但到目前为止,我只获得了用户的姓名和id。如{name:“Juan Fuentes”,id:“123456”} 我需要更多的信息,比如电子邮件、名字、姓氏和生日 这是我的js代码 function facebookLogin() { FB.login(function(response) { var token = response.authResponse.accessToken; var uid = response.a

我试图使用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:

声明性字段
为了尝试提高移动网络上的性能,v2.4中的节点和边缘需要显式请求GET请求所需的字段。例如,默认情况下,GET/v2.4/me/feed不再包括喜欢和评论,但是GET/v2.4/me/feed?fields=comments,likes将返回数据。有关更多详细信息,请参阅有关如何请求特定字段的文档

例如


还可以对来自公共_profile范围的数据使用此语法(在Graph API v2.9中测试):


您可以在图形API资源管理器中在线测试可能的值,只需单击“获取令牌”按钮:


以下是完整的脚本:

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'));
谢谢。

请注意,电子邮件并不总是通过调用
me
api并将
email
作为字段返回,即使范围
email
已被请求和批准,如果用户使用电话号码注册:


您可以编辑答案以激活针对此类问题的通知,因为facebook没有更新开发人员网站上的登录示例,这浪费了我几天的时间。:)如何发送多级字段,比如我想从范围中发送的提要api和我想从用户图片中发送的提要api?我花了好几天的时间才弄明白这一点。我使用Graph API v2.1继承了遗留代码,当FB于2017年7月10日停止支持它时,该代码突然崩溃。无法使用API升级工具查找要升级的内容,因为他们从中删除了v2.3及以下版本。如何使用新的显式版本从以下URL获取相册对象:{album_id}/photos?access_token={app_id}{124;{app_secret}
FB.api('/me?fields=birthday,link,gender,age_range', function(response) {
   console.log(response);
});
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'));