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

发布于 2024-09-12 19:31:25 字数 621 浏览 4 评论 0原文

我正在尝试使用新的 Graph API 做一件非常基本的事情。

我已经知道如何联系我的朋友了: “https://graph.facebook.com/me/friends?access_token=4333ed34d...”

但是如果我有一个 ID 为 123456 的朋友,那么我想获取的朋友: “https://graph.facebook.com/123456/friends?access_token=4333ed34d...”

但我得到一个例外:

“远程服务器返回错误:(500) 内部服务器错误。”

为什么我不能这样做?从 API 请求是一项非常简单的任务。

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..."

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..."

But I get an exception:

"The remote server returned an error: (500) Internal Server Error."

Why can't I do that? It's a very trivial task to ask from the API.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天荒地未老 2024-09-19 19:31:25

如果我尝试获取随机用户的朋友,我会得到 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"
   }
}

这是非常不言自明的。

如果我尝试让我朋友的朋友允许查看他的其他朋友,效果很好。如果我的朋友选择不允许查看他的其他朋友,我会得到同样的错误。

If I try to get friends of a random user I get HTTP 500 but it contains this response:

{
   "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.

‖放下 2024-09-19 19:31:25

你实际上可以从公共脸书上窃取信息。它并不漂亮,需要几秒钟,但可以工作。

我有一个从控制台运行并发出 AJAX 请求的 JS 代码 - 当您向下滚动时,在常规 facebook UI 中请求更多好友时,facebook 也会发出相同的请求(http://www.facebook.com/profile.php?sk=friends) 。然后我解析结果。到目前为止,它运行完美。我只是要求有更多的朋友,当我没有找到匹配的人时,我知道我已经拥有了他们。

我不想共享整个代码,但这是重要的部分:

// 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(); } } );
    }
}

您可以从当前登录的用户处获取必要的变量,如下所示:

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(); }
        });
    }

}

我知道这对您的情况可能没有用,因为这是 JS。无论如何,有人可能会发现这很有用。

PS: $jq = jQuery.
PPS:这些作业对象负责处理连续的 ajax 请求。我发现我需要它们,因为我的 FF 不想同时发出 2000+ AJAX 请求:-D

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

霓裳挽歌倾城醉 2024-09-19 19:31:25

你就是不能那样做。

如果您在用户授权您的应用程序时需要适当的扩展权限,您可以访问当前登录用户的朋友的一些数据,但这就是您获得的全部(http://developers.facebook.com/docs/authentication/permissions 请参阅:friends_xxxx 权限),但不是他/她的朋友。

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.

淡淡绿茶香 2024-09-19 19:31:25

我有朋友的朋友(有限)。我有同样的问题。虽然回答问题已经很晚了,但它会对某人有所帮助。这就是为什么回答这个问题。

我们可以找到应用程序用户的朋友的朋友。
它需要以下要求:

  1. 您的朋友需要使用应用程序(应用程序接受的权限)。
  2. 来自应用程序 read_stream、publish_stream、publish_checkins 的权限。

$fb_id= 朋友的朋友需要的用户ID。

尝试这个 fql 查询。

$query="SELECT uid, name, work_history FROM user WHERE uid IN (SELECT
uid2 FROM 朋友 WHERE uid1 IN (SELECT uid FROM user WHERE uid IN
(从朋友中选择 uid2,其中 uid1 = $fb_id )和 is_app_user=1))”;

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.

$fb_id= user id whose friends of friends required.

Try this fql query.

$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) )";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文