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

时间:2016-03-09 07:15:29

标签: node.js api outlook calendar

我正在尝试使用Rest API接受一个事件,但我无法获得成功的响应,得到这样的响应:{" statusCode":400," body": {"错误":{"代码":"错误无效请求","消息":"您的请求无法成为完成。您无法回复此会议,因为您是会议组织者。"}。

代码尝试直到:

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

任何帮助都将不胜感激。

提前致谢。

1 个答案:

答案 0 :(得分:0)

作为组织者,您无法在出席时回复此活动。您的出勤状态值为organizer

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

即使您在创建活动时将自己添加到与会者列表中,您也无法以出席情况作出回应,您将始终拥有出勤状态值none

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