2012-07-25 115 views
1

我正在編寫可與Google日曆同步的脫機日曆。它可以從Google日曆獲取數據,但不能將事件插入Google。這裏是我的插入代碼:Chrome擴展程序向Google日曆添加事件發生「解析錯誤」

var url = 'https://www.googleapis.com/calendar/v3/calendars/' + calendar_id + '/events'; 
    var request = { 
     'method': 'POST', 
     'headers': { 
      'GData-Version': '3.0', 
      'Content-Type': 'application/atom+xml' 
     }, 
     'body': { 
      'start': { 'dateTime': '2012-07-24T07:30:00+08:00'}, 
      'end': { 'dateTime': '2012-07-24T08:30:00+08:00'}, 
      'summary': calEvent.title, 
      'description': calEvent.body, 
      'attendees': [ { 'email': calendar_id}], 
      'reminders': { 
       'overrides': [ {'method': 'email', 'minutes': 15}] 
      } 
     } 
    }; 
    oauth.sendSignedRequest(url, function(resp) { console.log(resp) }, request); 

我已經多次檢查和搜索一些相關的問題,仍然無法找出是哪裏不對。這是返回錯誤:

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "parseError", 
    "message": "Parse Error" 
    } 
    ], 
    "code": 400, 
    "message": "Parse Error" 
} 
} 

回答

3

我弄清楚哪裏出錯了。請求的主體必須是字符串。

var body = { 
'start': { 'dateTime': '2012-07-24T07:30:00+08:00'}, 
      'end': { 'dateTime': '2012-07-24T08:30:00+08:00'}, 
      'summary': calEvent.title, 
      'description': calEvent.body, 
      'attendees': [ { 'email': calendar_id}], 
      'reminders': { 
       'overrides': [ {'method': 'email', 'minutes': 15}] 
      } 
     } 

然後轉動體成字符串:

body = JSON.stringify(body) 

設置請求變量:

request = { 
    ..... 
    'body': body 
    ..... 
} 
相關問題