如何使用Javascript SDK删除应用程序请求

时间:2011-10-21 20:27:22

标签: facebook-javascript-sdk access-token apprequests

我有以下Javascript代码:

        function RemoveRequest( requestToRemove )
        {           
            FB.api( requestToRemove, 'delete', function(response) {
                console.log(response);
              });
        }

这似乎是我看到人们使用的标准方法。不幸的是,我收到了一个错误。 我从中得到的响应对象说明如下: “必须指定应用程序请求收件人:必须通过用户签名的访问令牌或完全指定的应用程序请求ID指定此应用程序请求的收件人。”

所以,我尝试使用requestid_facebookid而不是运气。我尝试通过

传递我的访问令牌
FB.api(requestToRemove, 'delete', {access_token:accessToken}, function(response) {
                console.log(response);
              });

两者都给我同样的错误。

任何提示都将不胜感激。

编辑:为了清楚起见,我得到的authToken来自getLoginStatus的response.authResponse.accessToken。

3 个答案:

答案 0 :(得分:4)

我仍然不确定如何删除这些请求。但是,我确实想出了图api的解决方法。

新代码如下:

发送应用请求

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 );
        }
    });

并删除它

function RemoveRequest( requestToRemoveID, facebookUserID )
{
        requestToRemoveID = '/'+requestToRemoveID + '_' + facebookUserID;
    console.log( requestToRemoveID );

    FB.api( requestToRemoveID, 'DELETE', function(response) {
        console.log(response);
        });
}

这似乎会改变从用户应用请求到应用请求的请求类型。徽标已从我的个人资料图片更改为应用图标。我能够从图形api和facebooks网页中删除这些请求。如果有人有任何想法,我仍然想知道如何删除用户应用程序请求。

答案 1 :(得分:2)

我在网上搜索了6个小时,最后我自己想出来了。

发送请求:

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);
}

回调的两种可能方式:

选项1与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")

        }
}

选项2:使用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);
        }
    }
}

删除的回调函数:

private function onRequestDelete(result:Object, fail:Object):void{
    if(result == null){
        trace('Delete Request faild');
    }else{
        trace('Delete Request succes');
    }
} 

答案 2 :(得分:1)

如果您想使用Javascript SDK删除当前登录用户的所有Facebook待处理应用请求,您可以执行以下操作:

        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);
                        });
                    }
                });
            } 
        });