2011-08-26 46 views
1

我正在使用新的FB.ui为我的应用程序在Facebook上创建请求。如何从响应对象FB.ui获取受邀用户ID

用户与他们的朋友一起显示一个面板,他们选择他们想要发送请求的面板。

在回调函数中,我可以访问request_id,现在我想获取被邀请用户的详细信息。

如果我粘贴到浏览器下面的代码,我可以得到它的信息,我希望邀请的用户的USER_ID:

https://graph.facebook.com/138744992885393?access_token=193078857407882|d0048a9beb58a9a247c6b987.0-751640040|zNmBLfZxBBKikoj8RlZiHfKpugM 

我希望能够做的是做同样的事情,但从我的代码,然后访问返回的信息。

这里是我的代码:

function sendRequests() { 
     FB.ui({ 
      method: 'apprequests', 
      message: ' should learn more about this awesome site.', 
      data: 'extra data' 
     }, function(response) { 
      if (response != null && response.request_ids && response.request_ids.length > 0) { 
       for (var i = 0; i < response.request_ids.length; i++) { 
        alert("Invited: " + response.request_ids[i]); 
// somehow send a request to Facebook api to get the invited user id from the request_id and save to the database     
//can save these id's in the database to be used to track the user to the correct page in application. 

       } 
      top.location.href="http://localhost:3000/"; 
      } else { 
       alert('No invitations sent'); 
      } 
     }); 
    } 

我怎样才能做到这一点?

我使用Rails的Ruby 3.0.7 1.9.2

回答

1

你可以得到的Facebook用户ID如下:

for (var i = 0; i < req_ids.length; i++) { 

     alert("Invited: " + req_ids[i]); 
     FB.api('/me/apprequests/?request_ids='+toString(req_ids[i]), 
     function(response) 
     { alert(response); 
      alert(response['data'][0]['from']['id']); 
      }); 

           } 

非常感谢这个帖子on stackoverflow

+1

您可能还希望通过请求/ me/apprequests /?request_ids = {id1},{id2},{id3},{id4}来一次检查所有请求。这可能会更快。 – pixelastic

+0

谢谢Pixelastic我会尝试 – chell

0

如果是邀请用户的USER_ID,你可以让他们在你的回调,在response.request_ids。但是你似乎已经知道了。

你能澄清你的问题吗?

+0

如何获得用户ID返回Facebook好友名字的名字从response.request_id是我的问题? – chell

+0

哦,你是对的。我误解了你的问题。 chell给出比我更好的答案。 – pixelastic

1

您还可以得到Facebook的ID和甚至使用下面的代码选择朋友的名字:

FB.ui({

 method: 'apprequests', 
     redirect_uri: 'YOUR APP URL', 
     message: "Tom sent you a request" 
    },  
    function(response) { 
      if(response && response.hasOwnProperty('to')) { 
      for(i = 0; i < response.to.length; i++) { 
        //alert(response.to[i]); 
        // response.to[i] gives the selected facebook friends ID   
        // To get name of selected friends call this function below 
        getfbnames(response.to[i]); 
       }    
      } 
     } 
    ); 

乐趣ction getfbnames(selectedfrndid) {

  var url = 'getfriendfbname.php'; // call a php file through URL and jquery ajax   
    $.ajax({ 
    type:'POST', 
    url: url, 

    data : { fbid : selectedfrndid }, 
    async: false, 

    success: function(data) 
    { 
     alert(data); 
    } 
    }); // end of ajax 
} 

的文件getfriendfbname.php使用PHP的朋友Facebook ID

$fbid=$_POST['fbid']; 
$json = file_get_contents('https://graph.facebook.com/'.$fbid); 
$data = json_decode($json); 

echo $data->name; 
return $data->name; 
+0

谢谢,工作很完美。 – 2013-09-20 14:51:45