Get the Friends of my friend using the Graph API

后端 未结 4 480
有刺的猬
有刺的猬 2020-12-10 09:09

I am trying to do a very basic thing with the new Graph API.

I already know how to get my friends: \"https://graph.facebook.com/me/friends?access_token=4333ed34d...\

4条回答
  •  情深已故
    2020-12-10 09:28

    you can actualy steal the information from public facebook. It's not pretty, takes a couple seconds, but works.

    I have a JS code that runs from console and makes AJAX request - the same facebooks makes when requesting more friends in the regular facebook UI when you scroll down (http://www.facebook.com/profile.php?sk=friends). Then I parse the result. So far it works flawlessly. I just ask for more friends and when I don't get a match, I know I have them all.

    I don't want to share the whole code, but this is the essential part:

    // Recursively load person friends 
    function getMoreFriends(job, uid, fb_dtsg, post_form_id, offset, callback, finished ){
        var url = "http://www.facebook.com/ajax/browser/list/friends/all/?uid="+uid+"&offset="+offset+"&dual=1&__a=1&fb_dtsg="+fb_dtsg+"&lsd=&post_form_id="+post_form_id+"&post_form_id_source=AsyncRequest";  
        var request = { type: 'POST', url: url, data: { __a: 1, dual: 1, offset: offset, uid: uid }, dataType: "text", complete: function(data){
        var response = data.responseText.match(/HTML.*$/)[0];
            response = response.replace(/u003c/gi,"<");
            response = response.replace(/\\u([a-f0-9]{4})/gm, "&#x$1;").replace(/\\\//g,"/").replace(/\\/g,'');
            response = response.match(/^.*<\/div><\/div><\/div>/);
            if(response != null){
                response = response[0].replace("HTML(","");
                var people = [];
            $jq(response).find(".UIImageBlock").each( function(){
                var newPerson = new Person( $jq(this).find('.UIImageBlock_Content a').text(), $jq(this).find('a').first().attr('href'), $jq(this).find('img').attr('src'), jQuery.parseJSON( $jq(this).find('a').last().attr('data-gt') ).engagement.eng_tid );
                people.push( newPerson );
                });
                callback(people);
                getMoreFriends(job, uid, fb_dtsg, post_form_id, offset+60, callback, finished);
            }
        } };
        job.addToQueue( request );
        if(job.state != "processing"){
            if (typeof finished != "function" ){ finished = function(){}; }
            job.startProcessing({ finished: function(){ finished(); } } );
        }
    }
    

    You can get the neccesary variables from a currently logged in user like this:

    function loadFriends(person, onInit, store, callback){
        info("loading friends of "+person.name+" initiated");
        //addStatus("loading friends of "+person.name+" initiated");
    
        if (typeof onInit == "function" ){
            onInit();
        }
    
        if(person.id == -1){
            error("Person "+person.name+" doesn't have an id.!");
            addStatus("Person "+person.name+" doesn't have an id.!","error");
            return false;
        }
        else {
            // Load friends 
            var fb_dtsg = $jq('input[name="fb_dtsg"]').eq(0).val();
            var post_form_id = $jq('#post_form_id').val();
            var loadFriendsJob = ajaxManager.addJob({limit: 1});
            getMoreFriends(loadFriendsJob,person.id, fb_dtsg, post_form_id, 0,     function(people){ // callback on each iteration
                d( "Loaded "+people.length+" friends of " + person.name );
                store(people);
            },function(){ // callback on finish
                info("loading friends of "+person.name+" finished");
                //addStatus("loading friends of "+person.name+" finished");
                if (typeof callback == "function" ){ callback(); }
            });
        }
    
    }
    

    I understand this is probably useless for your case since this is JS. Anyway, someone might find this usefull.

    P.S.: $jq = jQuery. P.P.S.: those job objects take care of sequential ajax requests. I found out I need them since my FF didn't feel like making 2000+ AJAX request at the same time :-D

提交回复
热议问题