2011-08-26 66 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