简体   繁体   English

javascript功能来获取朋友,facebook api?

[英]javascript function for getting friends, facebook api?

I just wrote a test function, been learning to use the facebook api today and here is what I came up with. 我刚刚编写了一个测试函数,今天正在学习使用Facebook api,这就是我的想法。 I have a few questions, first let me show you the code. 我有几个问题,首先让我向您展示代码。

function getFriends() {
  FB.api('/me', function(response) {
      FB.api(response.id+'/friends', function(response) {
          $(document.body).prepend('<div class="test-list" style="position: fixed; z-index: 99999999; left: 50%; overflow: scroll; height: 500px; top: 25%;"></div>')
          for (var i=0; i < response.data.length; i++) { 
            FB.api('/'+response.data[i].id, function(response) {
              if(response.gender === 'male') {
                  $('.test-list').prepend('<li>'+response.name+'</li>');
              }
            });
          }         
      });
  });
}

Am I misusing the api by wrapping it like that? 我是通过这样包装来滥用api吗? This is the only way I could figure out how to access the users id then use that to get that users friends, after that I loop all the friends and filter only the males. 这是我弄清楚如何访问用户ID,然后使用该ID来获取用户朋友的唯一方法,此后,我循环所有朋友并仅过滤男性。

Now I can use this inside my backbone.js app and create or update a collection with this data. 现在,可以在我的ribs.js应用程序中使用它,并使用此数据创建或更新集合。

You can get Friends with one api call like this: 您可以通过一个api调用来获得Friends,如下所示:

function getFriends(){

    FB.api('/me?fields=friends.fields(gender,id,name)', function(response) {
      console.log(response);
    });

}

You can get a list of all friends by using "taggable_friends" keyword in API call, as follows. 您可以通过在API调用中使用“ taggable_friends”关键字来获取所有朋友的列表,如下所示。 The response gives you by default 25 friends/page. 默认情况下,响应为您提供25个朋友/页面。

FB.api("/me/taggable_friends",
  function (response) {
    if (response && !response.error) {
      /* handle the result */
    }
  }
);

Reference: https://developers.facebook.com/docs/graph-api/reference/v2.3/user/taggable_friends 参考: https : //developers.facebook.com/docs/graph-api/reference/v2.3/user/taggable_friends

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM