List of facebook friends

Asked

Viewed 828 times

2

Good people I’m having the following problem when trying to search the list of facebook friends of the logged in person I have the script done so that just login is not me to list the friends of the logged in person

I’d appreciate your help

Script

    <div id="result_friends"></div>
    <div id="fb-root"></div>

    <script>
    function sortMethod(a, b) {
        var x = a.name.toLowerCase();
        var y = b.name.toLowerCase();
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    }

    window.fbAsyncInit = function() {
        FB.init({ appId: '<?= $sApplicationId ?>', 
            status: true, 
            cookie: true,
            xfbml: true,
            oauth: true
        });

        function updateButton(response) {
            var button = document.getElementById('fb-auth');

            if (response.authResponse) { // in case if we are logged in
                var userInfo = document.getElementById('user-info');
                FB.api('/me', function(response) {
                    userInfo.innerHTML = '<img src="https://graph.facebook.com/' + response.id + '/picture">' + response.name;
                    button.innerHTML = 'Logout';
                });

                // get friends
                FB.api('/me/friends?limit=<?= $iLimit ?>', 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;
                });

                button.onclick = function() {
                    FB.logout(function(response) {
                        window.location.reload();
                    });
                };
            } else { // otherwise - dispay login button
                button.onclick = function() {
                    FB.login(function(response) {
                        if (response.authResponse) {
                            window.location.reload();
                        }
                    }, {scope:'email'});
                }
            }
        }

        // run once with current status and whenever the status changes
        FB.getLoginStatus(updateButton);
        FB.Event.subscribe('auth.statusChange', updateButton);    
    };

    (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    }());
    </script>
  • Welcome to Stackoverflow César. Our community model is different from forums, make a tour to better understand. If possible, include more details about what is happening and what you have tried to solve the problem. For example, is the return showing any message (error)? Create a minimum, complete and verifiable example can facilitate (who will answer your question) to find the problem.

  • I have tried to put in Scope users_friends to have permission to access friends but not solved it logs in for when to list friends does not appear anything. shows no error

  • Hello. Have you checked the browser console for any errors? Is your friend request response function being called? Did you test the query in the development tool to see if it is correct? Without a more specific mistake, it is very difficult for someone to help you. Take a look in this other answer I posted it myself. It’s about a slightly different need than yours, but it can help you verify that no step of Javascript communication has been forgotten.

2 answers

3

According to the Graph API documentation, the only friends that will be returned will be those who use the app. Otherwise, the list will be empty even.

Permissions

  • A user access token with user_friends permission is required to view the Current person’s Friends.
  • This will only Return any Friends who have used (via Facebook Login) the app making the request.
  • If a Friend of the person declines the user_friends permission, that Friend will not show up in the Friend list for this person.

0

Hello, from version 2.0 of the api the friends list is only shown if your app is in the games category.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.