我如何使用jsp文件中的Javascript从Facebook API获取Getfriends列表

How can I get Get friends list from Facebook API using Javascript in a jsp file

本文关键字:Facebook API 获取 列表 Getfriends Javascript 何使用 jsp 文件      更新时间:2023-09-26

当他登录我的网站时,我正试图获得一个用户基本配置文件和朋友列表,如(friendId、freindName等)。但目前我正在获取基本配置文件,我现在的问题是获取朋友列表并显示它。

这是我的代码

<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提前

我正在处理同样的问题。这是Facebook改变其API的结果。现在,只有使用同一应用程序的朋友才会出现在结果中。只有对于游戏开发商,才有一个额外的选项来邀请尚未成为同一应用程序已知用户的朋友。

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