简体   繁体   English

使用Graph API获取我朋友的朋友

[英]Get the Friends of my friend using the Graph API

I am trying to do a very basic thing with the new Graph API. 我正在尝试使用新的Graph API做一个非常基本的事情。

I already know how to get my friends: " https://graph.facebook.com/me/friends?access_token=4333ed34d ..." 我已经知道如何找到我的朋友:“ https://graph.facebook.com/me/friends?access_token=4333ed34d ......”

But if I have a friend who's ID is 123456, then I want to get his friends : " https://graph.facebook.com/123456/friends?access_token=4333ed34d ..." 但如果我的朋友的身份证号码是123456,那么我想得到他的朋友:“ https://graph.facebook.com/123456/friends?access_token=4333ed34d ......”

But I get an exception: 但我得到一个例外:

"The remote server returned an error: (500) Internal Server Error." “远程服务器返回错误:(500)内部服务器错误。”

Why can't I do that? 为什么我不能这样做? It's a very trivial task to ask from the API. 从API中提问是一项非常简单的任务。

If I try to get friends of a random user I get HTTP 500 but it contains this response: 如果我试图获得随机用户的朋友,我会获得HTTP 500,但它包含此响应:

{
   "error": {
      "type": "Exception",
      "message": "(#604) Can't lookup all friends of <UID>. Can only lookup for the logged in user (<MY_UID>), or friends of the logged in user with the appropriate permission"
   }
}

which is pretty self-explanatory. 这是非常明显的。

If I try to get friends of my friend who allows viewing his other friends it works fine. 如果我试图找到我的朋友的朋友,他允许查看他的其他朋友,它工作正常。 If my friend chose to not allow viewing his other friends I get the same error. 如果我的朋友选择不允许查看他的其他朋友,我会得到同样的错误。

you can actualy steal the information from public facebook. 你可以从公共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). 我有一个从控制台运行的JS代码并发出AJAX请求 - 当你向下滚动时,在常规facebook UI中请求更多朋友时,facebook会做同样的事情(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. 我知道这可能对你的情况毫无用处,因为这是JS。 Anyway, someone might find this usefull. 无论如何,有人可能会觉得这很有用。

PS: $jq = jQuery. PS:$ jq = jQuery。 PPS: those job objects take care of sequential ajax requests. PPS:这些作业对象负责顺序的ajax请求。 I found out I need them since my FF didn't feel like making 2000+ AJAX request at the same time :-D 我发现我需要它们,因为我的FF不想同时发出2000+ AJAX请求:-D

You just can't do that. 你不能这样做。

If you require the appropriate extended permission when the users authorize your app, you can access some data of the currently logged user's friends, but that's all you get ( http://developers.facebook.com/docs/authentication/permissions see: friends_xxxx permissions), but not his/her friends. 如果您在用户授权您的应用时需要相应的扩展权限,则可以访问当前登录用户的朋友的一些数据,但这就是您所获得的全部内容( http://developers.facebook.com/docs/authentication/permissions请参阅:friends_xxxx权限),但不是他/她的朋友。

I got friends of friends(limited). 我有朋友的朋友(有限)。 I had same problem. 我有同样的问题。 Though it is very late for answering question, it will help somebody. 虽然回答问题已经很晚了,但它会对某些人有所帮助。 That's why answering this question. 这就是回答这个问题的原因。

We can get friends of friends those are app users. 我们可以收到朋友的朋友那些应用用户。 It needs following requirements: 它需要以下要求:

  1. Your friend needs to be using application(accepted permissions for app). 您的朋友需要使用应用程序(应用程序的已接受权限)。
  2. Permission from application read_stream, publish_stream, publish_checkins. 应用程序read_stream,publish_stream,publish_checkins的权限。

$fb_id= user id whose friends of friends required. $ fb_id =朋友的朋友需要的用户ID。

Try this fql query. 试试这个fql查询。

$query="SELECT uid, name, work_history FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 IN (SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = $fb_id ) and is_app_user=1) )"; $ query =“SELECT uid,name,work_history FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 IN(SELECT uid FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = $ fb_id)and is_app_user = 1))”;

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

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