2016-03-09 170 views
2

我試圖接受使用REST API的事件,但我沒能獲得成功的響應,越來越響應這樣的一個事件:{「的StatusCode」:400,「身體」: {「錯誤」:{「代碼」:「ErrorInvalidRequest」,「消息」:「您的請求無法完成,不能參加本次會議,因爲你是會議組織者作出迴應。」}。OUTLOOK REST API接受如何使用Node.js

代碼嘗試,直到:

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

任何幫助,將不勝感激。

在此先感謝。

回答

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": "[email protected]" 
     } 
    },