繁体   English   中英

如何在jsp文件中使用Javascript从Facebook API获取“获取朋友”列表

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

当他登录到我的网站时,我试图获取用户的基本个人资料和朋友列表,例如(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>

拜托,我将不胜感激任何能提前帮助我的人

我正在处理同样的问题。 这是Facebook更改其API的结果。 现在,只有朋友会出现在使用同一应用程序的结果中。 仅对于游戏开发人员,还有一个附加选项可以邀请尚未是同一应用程序的已知用户的朋友。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM