【问题标题】:Facebook JS SDK's FB.api('/me') method doesn't return the fields I expect in Graph API v2.4+Facebook JS SDK 的 FB.api('/me') 方法不返回我在 Graph API v2.4+ 中期望的字段
【发布时间】:2016-08-22 20:47:04
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

    标签: javascript facebook


    【解决方案1】:

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

    声明性字段
    为了尝试提高移动网络的性能,v2.4 中的节点和边缘要求您明确请求 GET 请求所需的字段。例如 GET /v2.4/me/feed 默认不再包含 likes 和 cmets,但 GET /v2.4/me/feed?fields=cmets,likes 会返回数据。有关更多详细信息,请参阅有关如何请求特定字段的文档。

    例如

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

    【讨论】:

    • 您可以编辑答案以激活此类问题的通知developers.facebook.com/settings/developer/contact
    • 好吧,facebook 没有更新开发者网站上的登录示例,浪费了我好几天。 :)
    • 如何发送多级字段,例如我想要的提要 api 从范围和我想要的用户图片应该来?
    • 我花了好几天才弄明白。我使用 Graph API v2.1 继承了遗留代码,当 FB 在 2017 年 7 月 10 日停止支持它时突然崩溃。无法使用 API 升级工具寻求升级什么,因为他们从中删除了 v2.3 及更低版本。
    • 如何使用新的显式版本从如下 URL 获取相册对象:graph.facebook.com{album_id}/photos?access_token={app_id}|{app_secret}
    【解决方案2】:

    也可以对 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&path=me%3Ffields%3Dbirthday%2Clink%2Cgender%2Cage_range&version=v2.9

    【讨论】:

      【解决方案3】:

      这是完整的脚本:

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

      谢谢。

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-02
          • 2015-07-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多