我是Python的新手。我遇到了这个问题,希望你能提供帮助。让我解释一下我尝试做什么,如果我让你困惑,请告诉我。
我有这个Python脚本,它可以很好地创建一个事件。
# Set the request parameters
url = 'https://outlook.office365.com/api/v1.0/me/events?$Select=Start,End'
user = 'user1@domain.com'
pwd = getpass.getpass('Please enter your AD password: ')
# Create JSON payload
data = {
"Subject": "Testing Outlock Event",
"Body": {
"ContentType": "HTML",
"Content": "Test Content"
},
"Start": "2016-05-23T15:00:00.000Z",
"End": "2016-05-23T16:00:00.000Z",
"Attendees": [
{
"EmailAddress": {
"Address": "user1@domain.com",
"Name": "User1"
},
"Type": "Required" },
{
"EmailAddress": {
"Address": "user2@domain.com",
"Name": "User2"
},
"Type": "Optional" }
]
}
json_payload = json.dumps(data)
# Build the HTTP request
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(url, data=json_payload)
auth = base64.encodestring('%s:%s' % (user, pwd)).replace('\n', '')
request.add_header('Authorization', 'Basic %s' % auth)
request.add_header('Content-Type', 'application/json')
request.add_header('Accept', 'application/json')
request.get_method = lambda: 'POST'
# Perform the request
result = opener.open(request)
由于其他帖子建议为附件单独设置Json的属性(here),因此除了请求之外,我还包括下面的data_attachment代码(请参阅" data_attachment"和&#34 ; json_payloadAttachment&#34)。但是,我不确定如何在请求中添加它并进行一次POST。
# Set the request parameters
url = 'https://outlook.office365.com/api/v1.0/me/events?$Select=Start,End'
user = 'user1@domain.com'
pwd = getpass.getpass('Please enter your AD password: ')
# Create JSON payload
data = {
"Subject": "Testing Outlock Event",
"Body": {
"ContentType": "HTML",
"Content": "Test Content"
},
"Start": "2016-05-23T15:00:00.000Z",
"End": "2016-05-23T16:00:00.000Z",
"Attendees": [
{
"EmailAddress": {
"Address": "user1@domain.com",
"Name": "User1"
},
"Type": "Required" },
{
"EmailAddress": {
"Address": "user2@domain.com",
"Name": "User2"
},
"Type": "Optional" }
]
}
data_attachment = {
"@odata.type": "#Microsoft.OutlookServices.FileAttachment",
"Name": "test123.txt",
"ContentBytes": "VGVzdDEyMw=="
}
json_payload = json.dumps(data)
json_payloadAttachment = json.dumps(data_attachment)
# Build the HTTP request
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(url, data=json_payload) # NOT Sure where to put the attachment payload here
auth = base64.encodestring('%s:%s' % (user, pwd)).replace('\n', '')
request.add_header('Authorization', 'Basic %s' % auth)
request.add_header('Content-Type', 'application/json')
request.add_header('Accept', 'application/json')
request.get_method = lambda: 'POST'
# Perform the request
result = opener.open(request)
请帮忙。提前谢谢。
答案 0 :(得分:1)
您似乎需要合并数据;例如,您可以向数据字典中添加另一个名为Attachments
的密钥,其中包含一个字典数组并以这种方式合并它们;然后将您的数据序列化为JSON。
您不需要json_payloadAttachment
。
...
data["Attachments"] = [data_attachment]
json_payload = json.dumps(data)
根据您发布的链接,您也错过了HasAttachments
密钥。
data["HasAttachments"] = True