【问题标题】:Asking facebook for data of multiple users in single request using FB.api使用 FB.api 在单个请求中向 facebook 询问多个用户的数据
【发布时间】:2011-06-28 20:56:00
【问题描述】:

我需要使用FB.api('/<some_id>/') 从 Facebook 获取基本用户数据。这很好用,但速度很慢,因为我需要分别询问每个 id 并多次调用 facebook。有什么方法可以收集所有 id 并在单个请求中请求它们并获取一个数组?

编辑

我不是要用户朋友。我实际上是在尝试收集用户的朋友的朋友,而这是 Facebook 不提供的,这就是我使用 graph.facebook.com/<id>/ 而不是 graph.facebook.com/me/friends 的原因。我不要朋友,我想做这样的事情:graph.facebook.com/<id1>,<id2>,<id3>,../

【问题讨论】:

    标签: facebook


    【解决方案1】:

    您可以使用 fql 查询来获取所有朋友的信息:

    SELECT uid, name, pic_small FROM user 
    WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
    

    如果您提示用户提供更多扩展权限,您将能够访问来自user table 的更多字段。

    如果您尝试查询不是用户朋友的任意用户 ID 列表,您可以尝试在 FQL 查询中指定该列表以使用 Facebook 的 FQL Multiquery 功能,执行多个 select uid, name from user where uid = friendid1 语句。

    【讨论】:

    • 即使按照我认为您在编辑中的意思,这种 FQL 方法仍然有效。
    【解决方案2】:

    FQL怎么样:

    来自 Facebook 的示例:

    var friends = FB.Data.query(
         "(select uid2 from friend where uid1 = {0})",
         user_id);
    
    var friendsoffriends = FB.Data.query(
         "select uid2 from friend where uid1 in (select uid2 from {0})",
         friends;
    
     // Now, register a callback
     FB.Data.waitOn([friends, friendsoffriends], function() {   
       // now display all the results
       FB.Array.forEach(friendsoffriends.value, function(row) {
           console.log(row.uid);
       });
     });
    

    然后编辑您希望使用的任何 id 集合。

    上述方法仍然有效(作为一个合理的通用示例) - 但是,澄清一下:

    您之前会使用users.getInfo 来执行您所要求的操作,因为这允许您传递 UIDS 列表。不幸的是,这已被弃用。然而,这个查询实际上所做的只是包装一个 FQL 查询。

    如果您随后应用标准 FQL 'in' 语法,您可以实现:

    {
    myfriends:'select uid2 from friend where uid1=me()',
    friendsoffriends:'select uid2 from friend where uid1 in (select uid2 from #myfriends)'
    }
    

    但是 facebook 会主动阻止您执行此操作并出现错误:

    "error_code": 604,
      "error_msg": "Can't lookup all friends of 123456789. Can only lookup for the logged in user (987654321), or friends of the logged in user with the appropriate permission",
    

    您的另一个选择是获取所有 id 并执行以下形式的 FQL 查询:

    select name from user where uid='123456789' OR uid='987654321' OR uid=...
    

    【讨论】:

      【解决方案3】:

      FQL 不再受支持,但此方法在新 api 中可以解决问题:

      Requesting multiple Facebook fanpage data in a single call

      简而言之使用ids参数:

      FB.api(
          '/', 
          {ids : "19292868552,11239244970"}, 
          function(response) { 
              if (!response || response.error) {
                  console.log(response.error); 
              } else { 
                  console.log(response); 
              } 
          }
      );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-21
        • 1970-01-01
        • 2018-09-16
        • 1970-01-01
        • 2019-05-06
        • 2019-04-09
        • 1970-01-01
        相关资源
        最近更新 更多