2014-10-30 12 views
0

我想获得一个用户基本的个人资料和朋友列表像(friendId,freindName等),当他登录到我的网站。但现在我得到的基本简介和我现在的问题是要得到朋友列表并显示它。我怎样才能得到Facebook API使用Javascript在jsp文件中的朋友列表

这里是我的代码

<body> 
    <script> 
     // This is called with the results from from FB.getLoginStatus(). 
     function statusChangeCallback(response) { 
     console.log(response); 
     if (response.status === 'connected') { 
      // Logged into your app and Facebook. 
      testAPI(); 
     } else if (response.status === 'not_authorized') { 
      // The person is logged into Facebook, but not your app. 
      document.getElementById('status').innerHTML = 'Please log ' + 'into this app.'; 
     } else { 

      document.getElementById('status').innerHTML = 'Please log ' + 'into Facebook.'; 
     } 
     } 

     // This function is called when someone finishes with the Login 
     // Button. See the onlogin handler attached to it in the sample 
     // code below. 
     function checkLoginState() { 
     FB.getLoginStatus(function(response) { 
      statusChangeCallback(response); 
     }); 
     } 

     //this is to initialize the 
     window.fbAsyncInit = function() { 
     FB.init({ 
     appId  : 'my_app_id', 
     cookie  : true, // enable cookies to allow the server to access the session 
     xfbml  : true, // parse social plugins on this page 
     version : 'v2.1' // use version 2.1 
     }); 

     FB.getLoginStatus(function(response) { 
     statusChangeCallback(response); 
     }); 
    }; 

     // Load the SDK asynchronously 
     (function(d, s, id) { 
     var js, fjs = d.getElementsByTagName(s)[0]; 
     if (d.getElementById(id)) return; 
     js = d.createElement(s); js.id = id; 
     js.src = "//connect.facebook.net/en_US/sdk.js"; 
     fjs.parentNode.insertBefore(js, fjs); 
     }(document, 'script', 'facebook-jssdk')); 

     // Here we run a very simple test of the Graph API after login is 
     // successful. See statusChangeCallback() for when this call is made. 
     function testAPI() { 
     console.log('Welcome! Fetching your information.... '); 
     FB.api('/me', function(response) { 
      console.log('Successful login for: ' + response.name); 
      document.getElementById('status').innerHTML = 'User id, ' + response.id + '!'; 
      document.getElementById('status1').innerHTML = 'User name, ' + response.name +'!'; 
     }); 

//the sort function to sort the list of friends 
     function sortMethod(a, b) { 
      var x = a.name.toLowerCase(); 
      var y = b.name.toLowerCase(); 
      return ((x < y) ? -1 : ((x > y) ? 1 : 0)); 
     }  

    // now to get friends where my problem is found 
     //var limit = 10; 
     FB.api('/me/friends', function(response) { 
      var result_holder = document.getElementById('result_friends'); 
      var friend_data = response.data.sort(sortMethod); 
      var results = ''; 
        for (var i = 0; i < friend_data.length; i++) { 
         results += '<div><img src="https://graph.facebook.com/' + friend_data[i].id + '/picture">' + friend_data[i].name + '</div>'; 
        } 
     // and display them at our holder element 
      result_holder.innerHTML = '<h2>Result list of your friends:</h2>' + results; 
     }); 

     } 
    </script> 
    <div class="fb-login-button" 
      scope="public_profile, email, user_friends" 
      onlogin="checkLoginState();" 
      data-auto-logout-link="true"> 
    </div> 

    // here it's working 
    <div id="status"></div> //so i get the user id 
    <div id="status1"></div> //and here the user name as espected 
    <div id="fb-root"></div> 

    //this is to display the like button and share no problem and it also working 
    <div 
     class="fb-like" 
     data-share="true" 
     data-width="450" 
     data-show-faces="true" 
     data-size="large"> 
     </div> 

    // But here nothing is displaying and nothing like error in the console 
    <div id="result_friends"></div> 

请我真的很感激任何人谁可以给我一个手 THKS提前

+0

你是否在朋友数据中获得空阵列? – 2014-10-30 18:18:21

+0

你应该考虑你的问题,JS和JSP在不同的方面进行操作。 – yunandtidus 2014-10-30 22:22:59

+0

是的阿卜杜勒我得到一个空阵列 – kamdjou 2014-10-31 09:48:49

回答

0

我处理同样的问题。这是Facebook对其API改变的结果。现在只有朋友会在使用同一个App的结果中。只有游戏开发者有另外的选择邀请那些还不是已知用户的朋友。

如果您的应用不是游戏,那么从一开始就很难扩大您的客户群。

相关问题