2012-02-12 25 views
0

友達のIDを取得できたとしましょう。 (全員がFacebook上にいるユニークな番号)Facebook APIを使用して友人の名前を取得する方法

どのようにしてその友達の名前を表示できますか?

このようなjavascript警告ボックスを作りたいと思います。

<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> 
関連する問題