2012-02-12 57 views
0

假設我能夠獲得朋友的身份證。 (這是Facebook上每個人都有的唯一號碼。)如何使用Facebook API獲取朋友的姓名

如何顯示該朋友的姓名?

我想做一個這樣的JavaScript警報框。

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

在上面的例子中,我將如何獲得他們的名字到name變量中?

回答

3

您需要使用圖形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> 
相關問題