2016-12-01 90 views
0

即時得到錯誤的Python/JSON - KeyError異常「身體」創建

>>> appt = CreateEvent(authentication, result[0].calendarId, subject_text, start_time, end_time, subscribers, content) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 9, in CreateEvent 
KeyError: 'Body' 

的下方,代碼時,即時通訊新的JSON所以不明白爲什麼這個心不是工作。所有其他工作,但我看到身體上的錯誤,是因爲即時通訊使用兩個[]? 這是要發送到Office 365 API,所以它期望它的格式。

感謝

def CreateEvent(auth, calendar, subject, start_time, end_time, attendees, content): 
    create_url = 'https://outlook.office365.com/api/v1.0/me/calendars/{0}/events'.format(calendar) 
    headers = {'Content-type': 'application/json', 'Accept': 'application/json'} 
    data = {} 
    data['Subject'] = subject 
    data['Start'] = start_time 
    data['End'] = end_time 
    data['Attendees'] = attendees 
    data['Body']['Content'] = content 
    data['Body']['ContentType'] = 'Text' 
    content_data = json.dumps(data) 
+0

好像'data'沒有鍵'Body' –

回答

3

你首先需要在你的data doctionary Body關鍵。你可以這樣解決你的問題:

data = {'Body': {}} 

data['Subject'] = subject 
data['Start'] = start_time 
data['End'] = end_time 
data['Attendees'] = attendees 
data['Body']['Content'] = content 
data['Body']['ContentType'] = 'Text' 
# ...