Get a List of All Friends on Facebook Using JS SDK

2019-07-26 14:58发布

问题:

I am trying to get a list of all friends of the current user using my Facebook application:
Here is my code for your reference:

    var meID = <?php echo $data['user_id']; ?>;
    var queryOnFriends = "SELECT uid, name, online_presence, status FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = " + meID + ")";
    var queryOnFriends1 = "SELECT uid, name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=" + meID + ")";
    var queryOnFriends2 = "SELECT uid2 FROM friend WHERE uid1 = me()";
    var queryUserName = "SELECT name, uid FROM user WHERE uid={0}";
    var query = FB.Data.query(queryOnFriends2, meID);
    query.wait(function(rows){
        alert("in callback");
        document.getElementById('display').innerHTML =
        'Your name is ' + rows[0].name;
    });

Referring to my attempt using var queryUserName works fine, but all the others didn't work, even the simple queryOnFriends2 from the FQL examples.
The application asks for permissions correctly (read_friendlists, user_online_presence, friends_online_presence) and the FB JS SDK seems to initialize well, as querying for the user's own username (queryUserName) works, however, it does not even execute the callback function in the failing cases!

What am I doing wrong?

回答1:

The difference between queryUserName and your other queries is that queryUserName contains a parameter, the {0}.

Your other queries are using me() instead, so they don't need a data parameter to be passed in.

You need to change this line:

var query = FB.Data.query(queryOnFriends2, meID);

to this:

var query = FB.Data.query(queryOnFriends2);


回答2:

OK i got it now ... i used php to authorize my app ... but didn't authenticate as well! When using just the facebook jsSDK and initalize and then getStatus it does it all automaticly!

Then the queries worked!