如何使用Facebook API获取朋友的姓名

How to use Facebook API to get the name of a friend

本文关键字:朋友 获取 何使用 Facebook API      更新时间:2023-09-26

假设我能够获得一个朋友的id。(每个人在Facebook上都有一个独特的号码。)

如何显示该朋友的姓名?

我想制作一个这样的javascript提醒框。

<script type="Javascript/text">
function showname(id)
{
alert("The name of your friend with id " + id + " is " + name)
}
</script>

在上面的例子中,我如何将它们的名称输入到名称变量中?

您需要像这样使用Graph API:

 <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId   : 'JUST_REPLACE_THIS_WITH_THE_APP_ID',
          cookie  : true,
          status  : true,
          xfbml   : true 
        });
        FB.Event.subscribe('auth.login', function(response) {
          window.location.reload();
        });
        FB.api('/me/friends',function(resp) {
            if(resp && resp.data.length) {
                var html = '<ul>';
                for(var i=0; i<resp.data.length; i++) {
                    html += '<li><img src="http://graph.facebook.com/' + resp.data[i].id + '/picture?type=small" />' + resp.data[i].name + '</li>';
                }
                html += '</ul>';
                document.getElementById('friends').innerHTML = html;
            }
        });
      };
      (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>

您可以将OAuth库用于FB或使用cURL。如果你使用cURL,你只需要向https://graph.facebook.com/id并返回name作为参数。参考http://developers.facebook.com/docs/reference/api/