node.js - OUTLOOK REST API 使用 Node.js 接受事件

标签 node.js api outlook calendar

我尝试使用 Rest API 接受事件,但无法获得成功的响应,得到如下响应:{"statusCode":400,"body":{"error":{"code": "ErrorInvalidRequest","message":"您的请求无法完成。您无法回复此 session ,因为您是 session 组织者。"}。

代码尝试直到:

    function getCalendarOfUser(email, token, startDate, endDate, iCalUId, callback) {
       console.log("retrieved Start Date: "+startDate+"  ,End date:  "+endDate);
       // startDate = '2016-03-10T10:27:00';
        //endDate = '2016-05-10T11:27:00';
        console.log("Email found in cookie: ", email);
        console.log("start date : ", startDate + " end date : " + endDate);
        var optionsForGettingcalendars = {
           // uri: 'https://outlook.office365.com/api/v2.0/me/calendarview?startDateTime=' + startDate + '&endDateTime=' + endDate,
            uri:'https://outlook.office.com/api/v2.0/me/events?$select=Subject,Organizer,Attendees,Start,End,iCalUId',
            port: 443,
            method: 'GET',
            headers: {
                'Authorization': 'Bearer ' + token,
                'Content-Length': 0
            },        
            resolveWithFullResponse: true,
            simple: false
        };

        rp(optionsForGettingcalendars)
            .then(function(response) {          
                console.log("iCalUId to match : " + iCalUId);
                if (response.statusCode == 200 || response.statusCode == 201) {
                    var events = response.body.value;
                    var my_body = JSON.parse(response.body);
                    my_body = my_body.value;               
                    console.log("EVENTS[]  =========:: " + JSON.stringify(my_body) + '\n');
                    for (var i = 0; i < my_body.length; i++) {                  
                        console.log("Matching My Calender Event:   "+my_body[i].iCalUId + "     to match with " + iCalUId);
                        if (my_body[i].iCalUId == iCalUId) {
                             console.log("MATCHED Event to ACCEPT/REJECT : " + JSON.stringify(my_body[i]) + '\n');
                            var result = {
                                id: my_body[i].Id,
                                message: "success"
                            };
                            return callback(null, result);
                        }
                    }
                    var error = {
                        message: "error no match found fo calendar"
                    };

                    return callback(error, null);
                } else if (response.statusCode == 401) {
                    var error = {
                        message: "refresh token"
                    };

                    return callback(error, null);
                }else{
                     //var my_body = JSON.parse(response);

                     console.log("Error Response:   : " +  JSON.stringify(response));
                }
            })
            .catch(function(err) {
                var error = {
                    message: "error getting calendars"
                };
                console.log("In Catch Block  error" + JSON.stringify(err));
                //return callback(error,null);

            });  
    }

accept Event API:
var requestBody = JSON.stringify({
    "Comment": reasonForAccept,
    "SendResponse": "true"
});
var optionsForDecliningEvent = {
    //uri: 'https://outlook.office365.com/api/v2.0/me/events/' + result.id + '/accept',
    uri: 'https://outlook.office.com/api/v2.0/me/events/' + result.id + '/accept',
    port: 443,
    method: 'POST',
    headers: {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(requestBody)
    },
    json: true,
    body: {
    "Comment": reasonForAccept,
    "SendResponse": "true"
    },
    resolveWithFullResponse: true,
    simple: false
};
var success=false;
rp(optionsForDecliningEvent)
.then(function(response) {
    console.log("response after accepting event : " + JSON.stringify(response)+"  ,response.statusCode :"+response.statusCode);
    if (response.statusCode == 200 || response.statusCode == 201|| response.statusCode == 202) {           
        logger.MessageQueueLog.log("info","index.cs:-Outlook:   successfully accepted event"); 
        mongoapi.updateDynamicDocuments("conference_details",where_query,{$pullAll:{'rejected_participants':[employee_id]},$addToSet:{'participant_ids': {$each:[employee_id]}}}, {multi:true},function(err,updateconfDetlArray){
            if(err){
                logger.MessageQueueLog.log("error","Outlook:Accept-  MongoDB:- Unable to update Accepted Employee details into mongodb.Error is "+err);

            }
            else{
                logger.MessageQueueLog.log("info","Outlook:Accept-  MongoDB:-Accepted Updated Conference_details collection: "+JSON.stringify(updateconfDetlArray));
            }
        });
        success=true;                                               
        var result = {
            type: "success"                                
        };                                                      
        return callback(null,result);
    }
    else{
        logger.MessageQueueLog.log("info","index.cs:-Outlook:Accept- Accepting Invitation Code,response.statusCode :"+response.statusCode);
    }
})
.catch(function(err) {
    logger.MessageQueueLog.log("Error","index.cs:-Outlook:Accept- error in Catch Block: " + JSON.stringify(err));
    if(success){
        logger.MessageQueueLog.log("info","index.cs:-Outlook:Accept-    successfully accepted event in catch block");                                                    
        var result = {
            type: "success"                                
        };                                                      
        //return callback(null,result);
    }else{
        //success = false;
        logger.MessageQueueLog.log("info","index.cs:-Outlook:Accept-    else of successfully accepted event in catch block");
        var error = {
            message: "error getting calendars"
        };
        //return callback(error,null);
    }
})

如有任何帮助,我们将不胜感激。

提前致谢。

最佳答案

作为组织者,您无法通过出席来回应事件。您的出席状态值组织者:

"responseStatus": {
    "response": "organizer",
    "time": "0001-01-01T00:00:00Z"
},

即使您在创建事件时将自己添加到与会者列表中,您也无法响应出席,您的出席状态值将始终为:

"attendees": [
    {
        "status": {
            "response": "none",
            "time": "0001-01-01T00:00:00Z"
        },
        "type": "required",
        "emailAddress": {
            "name": "Name Surname",
            "address": "name_surname@somewhere.com"
        }
    },

关于node.js - OUTLOOK REST API 使用 Node.js 接受事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35885264/

相关文章:

javascript - 从其他文件获取数据库信息

node.js - 在 Docker 容器内或外部运行 Grunt/Gulp?

php - 通过 Dropbox Chooser 的非过期下载链接

javascript - javascript中的JSON解析问题

vba - 在 Outlook 2010 中的新邮件消息功能区中切换自定义按钮

node.js - 在 Node.JS 中,从大型对象而不是硬盘驱动器提供文件有什么优势吗?

javascript - jQuery POST 到 Node.JS 失败

vba - 在 64 位 VBA 中使用 TaskDialogIndirect

performance - 为什么 Outlook 关闭时 Excel VBA 的运行速度明显加快?

java - 使用 Java 代码打开 Outlook 邮件客户端,其中包含“收件人”、“抄送”、“主题”和“正文”