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

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

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

但如果我的朋友的身份证号码是123456,那么我想得到他的朋友:“ https://graph.facebook.com/123456/friends?access_token=4333ed34d ……”

但我得到一个例外:

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

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

如果我试图获得随机用户的朋友,我会收到HTTP 500,但它包含此响应:

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

这是非常明显的。

如果我试图找到允许查看其他朋友的朋友的朋友,那就可以了。 如果我的朋友选择不允许查看他的其他朋友,我会得到同样的错误。

你可以从公共Facebook窃取信息。 它不漂亮,需要几秒钟,但有效。

我有一个从控制台运行的JS代码并发出AJAX请求 – 当你向下滚动时,在常规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

你不能这样做。

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

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

我们可以收到朋友的朋友那些应用用户。 它需要以下要求:

  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 friend WHERE uid1 IN(SELECT uid FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = $ fb_id)and is_app_user = 1))”;