2017-02-14 58 views
0

我已經檢查了與此問題有關的其他帖子,他們是不一樣的:我使用「響應」,我從谷歌api網站複製日期/時間,所以格式是正確的,我已經玩過頭。任何其他想法爲什麼這不起作用?谷歌日曆事件API插入投擲400

,我發現了以下錯誤:

Error: failed [400] { "error": { "errors": [ {  "domain": "global",  "reason": "required",  "message": "Missing end time." } ], "code": 400, "message": "Missing end time." } } [object Object] 

我的代碼下面細分爲以下步驟:網址我要去POST,用戶訪問令牌,對包括報頭插入的日曆事件的選項以及事件的開始,結束和事件總結,以及實際的http post和callback函數:

calendarSchedule() { 
    if(Meteor.user() && moment(Meteor.user().services.google.expiresAt) > moment()._d) { 
     var url = "https://www.googleapis.com/calendar/v3/calendars/primary/events"; 
     var userAccessToken = Meteor.user().services.google.accessToken; 

     var options = { 
     headers : { 
      'Content-Type': 'application/json', 
      'Authorization': 'Bearer ' + userAccessToken, 
      'X-JavaScript-User-Agent': "Google APIs Explorer", 
     }, 
     calendarId: 'primary', 
     resource : { 
      start: { dateTime: "2016-05-03T18:03:58+02:00" }, 
      end: { dateTime: "2016-05-03T18:03:58+02:00" }, 
      summary: "testSummar", 
     } 
     }; 

     HTTP.post(url, options, 
     function(error,result) { 
      console.log("posted to calendar? "+ error+ result); 
     }); 
    } 
    } 

回答

0

我想通了,我想我應該在這裏發佈答案。

出於某種原因,在這種情況下,您使用數據而不是資源。我不確定爲什麼如果有人想要在這一點上發表意見,但這與谷歌API日曆網站上的內容不同。

我的作品最終代碼:

calendarSchedule() { 
    if(Meteor.user() && moment(Meteor.user().services.google.expiresAt) > moment()._d) { 
     var url = "https://www.googleapis.com/calendar/v3/calendars/primary/events"; 
     var userAccessToken = Meteor.user().services.google.accessToken; 

     var currentEvent = { 
     'summary': this.props.text, 
     // 'location': this.refs.location.textContent, 
     'start': { 
      'dateTime': moment()._d, 
      'timeZone': this.refs.timeZone, 
     }, 
     'end': { 
      'dateTime': moment(moment()._d).add(1, 'hours'), 
      'timeZone': this.refs.timeZone, 
     }, 
     'attendees': [], 
     'reminders': { 
      'useDefault': true, 
     } 
     }; 

     var options = { 
     headers : { 
      'Content-Type': 'application/json', 
      'Authorization': 'Bearer ' + userAccessToken, 
      'X-JavaScript-User-Agent': "Google APIs Explorer", 
     }, 
     calendarId: 'primary', 
     data: currentEvent, 
     }; 

     var searchResult = HTTP.post(url, options, 
     function(error,result) { 
      console.log("posted to calendar? "+ error+ result); 
     }); 
    } 
    }