如何得到朋友的名字

时间:2016-08-12 04:49:28

标签: facebook facebook-graph-api facebook-javascript-sdk

我正在使用此代码:

   function login() {

       FB.login(function(response) {
           if (response.authResponse) {
               console.log('Welcome! Fetching your information.... ');

               FB.api('/me/friends', {
                   fields: 'id,name'
               }, function(response) {
                   for (element in response) {
                       console.log(element.name);
                   }
               });


           } else {
               console.log('User cancelled login or did not fully authorize.');
           }
       }, {
           'scope': 'user_friends'
       });
   }

但是我在控制台中得到了这个回复:

Welcome! Fetching your information.... 
2 undefined

为什么?

编辑:

我刚编辑了上面的代码:

   function login() {

       FB.login(function(response) {
           if (response.authResponse) {
               console.log('Welcome! Fetching your information.... ');

               FB.api('/me/friends', {
                   fields: 'id,name'
               }, function(response) {
                   console.log(response.summary);
                   console.log(response);
                   for (element in response.data) {
                       console.log(element.id);
                   }
               });


           } else {
               console.log('User cancelled login or did not fully authorize.');
           }
       }, {
           'scope': 'user_friends'
       });
   }

但现在我收到了这个回复:

Welcome! Fetching your information.... 
Object {total_count: 58}
Object {data: Array[0], summary: Object}

1 个答案:

答案 0 :(得分:1)

再也找不到朋友了。您也只能获得使用user_friends权限授权您的应用的朋友。

这将是正确的代码:

FB.api('/me/friends', {
   fields: 'id,name'
}, function(response) {
   for (element in response.data) {
       console.log(element.name);
   }
});

更多信息:Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app