Oscar
Oscar

Reputation: 1103

how to get user id on facebook request dialog

What i want to do is get the request ID and insert to my database. My request dialog is run well, but the problem is I cannot get 'TO' user id http://developers.facebook.com/docs/reference/dialogs/requests/ , but I still cannot get the request user ID.

here is my coding:

         function newInvite(){
            //var user_ids = document.getElementsByName("user_ids")[0].value;
           FB.ui({ 
                    method : 'apprequests',
                    title: 'X-MATCH',
                    message: 'Come join US now, having fun here',                       
            },                      
            function getMultipleRequests(requestIds) {
              FB.api('', {"ids": requestIds }, function(response) {
                console.log(response);
              });
            }                   
            );
        }

any solution on this?

million thanks for help

Upvotes: 2

Views: 4653

Answers (1)

Hanon
Hanon

Reputation: 3927

Have you enable request 2.0 efficient?

If you have enabled, you can get the user id easily as the response like this

{
    request: ‘request_id’
    to:[array of user_ids]
}

In your callback function, you can use

response.request to get the request ID

response.to to get the array of user ids

And notice that if you use request 2.0, the request ID format will like this

<request_object_id>_<user_id>

If you doesn't enable it, then you can only get the array of request ids and you need to make another api call to get the user id

Edited:

FB.ui({
        method : "apprequests",
        title : "your title",
        message : "your msg"
      },
      function(response)  {
          var receiverIDs;
          if (response.request)  {
              var receiverIDs = response.to;  // receiverIDs is an array holding all user ids
          }
      }
);

Then you can use the array "receiverIDs" for further process

For example I sent a request to user id with id "1234", "5678"

The response will like this:

{
    request: ‘1234567890’   // example,
    to:['1234', '5678'] 
}

In request 2.0, the full request id will look like this

1234567890_1234
1234567890_5678

Caution: FB doc tell you to manage and delete the request yourself, if you using request 2.0, remember to delete the id like the above, if you directly delete the request '123456789', all the full request ID with this prefix will be deleted.

==================================================================

If you haven't enable request 2.0, follow the code on the doc page to see how to get the user id by making a api call

function getMultipleRequests(requestIds) {
  FB.api('', {"ids": requestIds }, function(response) {
    console.log(response);
  });
}

The response format for these methods is as follows:

{
   "id": "[request_id]",
   "application": {
      "name": "[Application Name]",
      "id": "[Application ID]"
   },
   "to": {
      "name": "[Recipient User Name]",
      "id": "[Recipient User ID]"
   },
   "from": {
      "name": "[Sender User ID]",
      "id": "[Sender User Name]"
   },
   "message": "[Request Message]",
   "created_time": "2011-09-12T23:08:47+0000"
}

you can implement the callback of api call and getting who is the receiver by response.to.id

Upvotes: 4

Related Questions