How to delete app requests with Javascript SDK
-
27-04-2021 - |
Question
I've got the following Javascript code:
function RemoveRequest( requestToRemove )
{
FB.api( requestToRemove, 'delete', function(response) {
console.log(response);
});
}
This seems to be the standard approach I see people using. Unfortunately, I get an error. The response object I get from this states the following: "App Request Recipient Must Be Specified: The recipient for this app request must be specified through a user-signed access token or the fully specified app request ID."
So, I try using requestid_facebookid instead... no luck. I try passing in my access token via
FB.api(requestToRemove, 'delete', {access_token:accessToken}, function(response) {
console.log(response);
});
Both give me the same error.
Any tips would be greatly appreciated.
Edit: Just for clarity's sake, the authToken I am getting is from response.authResponse.accessToken from getLoginStatus.
Solution
I'm still not sure how to delete these requests. However, I did come up with a workaround with the graph api.
New code is as follows:
Sending the app request
var params = {};
params['message'] = 'My message!';
params['title'] = 'My title!';
params['to'] = inID;
params['access_token'] = accessToken;
FB.api('/me/apprequests', 'POST', params,
function(response){
if( response )
{
console.log( response );
}
});
And deleting it
function RemoveRequest( requestToRemoveID, facebookUserID )
{
requestToRemoveID = '/'+requestToRemoveID + '_' + facebookUserID;
console.log( requestToRemoveID );
FB.api( requestToRemoveID, 'DELETE', function(response) {
console.log(response);
});
}
This seemed to change the type of the request from a user app request to an app request. The logo changed from my profile picture to the app icon. I was able to delete these requests both from the graph api and through facebooks webpage. I'd still love to know how to delete the user app requests if anyone has any ideas.
OTHER TIPS
I lost 6 hours searching online, at the end I figured it out by myself.
send request:
private function invite():void
{
var dat:Object = new Object();
dat.message = "MyMessage";
dat.title = "MyTitle;
dat.filters = ['app_non_users'];
Facebook.ui('apprequests', dat, onUICallback);
}
2 possible way for the callback:
Option 1 with Facebook.api
private function onUICallback(result:Object):void{
if(result == null){
trace('User closed the pop up window without inviting any friends');
}else{
if(result.hasOwnProperty("request")){
var data:Object = new Object();
data.method = 'delete';
var userid:String = "here goes the uid of the user that sent the request"
var fullrequestID:String = result.request + '_' + userid;
Facebook.api(fullrequestID, onRequestDelete, data, "POST")
}
}
option 2: with Facebook.deleteObject
private function onUICallback(result:Object):void{
if(result == null){
trace('User closed the pop up window without inviting any friends');
}else{
if(result.hasOwnProperty("request")){
var userid:String = "here goes the uid of the user that sent the request"
var fullrequestID:String = result.request + '_' + userid;
Facebook.deleteObject(fullrequestID, onRequestDelete);
}
}
}
callback function for the delete:
private function onRequestDelete(result:Object, fail:Object):void{
if(result == null){
trace('Delete Request faild');
}else{
trace('Delete Request succes');
}
}
If you want to remove all the Facebook pending app requests of the current logged user with the Javascript SDK you can do the following:
FB.getLoginStatus(function(response) {
console.log(response);
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
FB.api('/me/apprequests', function(response){
console.log( response );
for (var i = 0; i < response.data.length; i++){
FB.api(response.data[i].id, 'delete', function(response) {
console.log(response);
});
}
});
}
});