2012-02-12 3 views
0

친구의 ID를 얻을 수 있다고 가정 해 봅시다. (모든 사람들이 Facebook에 가지고있는 고유 번호)Facebook API를 사용하여 친구의 이름을 얻는 방법

어떻게 그 친구의 이름을 표시 할 수 있습니까?

이렇게 자바 스크립트 경고 상자를 만들고 싶습니다.

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

위의 예에서 이름 변수에 이름을 어떻게 넣을 수 있습니까?

답변

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> 
관련 문제