如何从响应对象FB.ui获取受邀用户ID

时间:2011-08-26 06:47:53

标签: ruby-on-rails-3 facebook

我正在使用新的FB.ui在Facebook上为我的应用程序创建请求。

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

在回调中,我可以访问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 3.0.7 Ruby 1.9.2

3 个答案:

答案 0 :(得分: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 :(得分: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]);
                }               
            }
       }
     );

function 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返回facebook朋友姓名的名称

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

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

答案 2 :(得分:0)

如果这是受邀用户的user_id,您可以在回调中将它们放在response.request_ids中。但你似乎已经知道了。

你能澄清一下你的问题吗?