我正在使用下面的Python脚本为Office 365创建事件并且它可以正常工作。但是,我有一段艰难的时间(谷歌搜索一整天),以了解如何将附件包含在下面此脚本生成的事件中。
以下代码适用于创建活动。
123
但是当我尝试包含附件时,它无效(请参阅下面的附件代码)
# 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)
接下来,我尝试将该过程分开。所以,首先我创建了事件并捕获了eventID。然后尝试在URL中包含eventID(见下文),但仍然没有运气。
# 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" }
],
"Attachments": [
{
"@odata.type": "#Microsoft.OutlookServices.FileAttachment",
"Name": "menu.txt",
"ContentBytes": "JVBERi0xLjMNCjEgMCBvYPRg=="
}
],
"HasAttachments":"true"
}
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)
任何帮助都会非常感激。
答案 0 :(得分:2)
要为活动创建附件,我们需要发送另一个请求。以下是现有事件的creating the attachment说明。
创建附件后,我们还需要更新事件,以便其他人可以看到更改。
以下是为事件创建附件的示例:
创建活动:
POST: https://outlook.office.com/api/v2.0/me/events
authorization: bearer {token}
content-type: application/json
{
"Subject": "Discuss the Calendar REST API",
"Body": {
"ContentType": "HTML",
"Content": "I think it will meet our requirements!"
},
"Start": {
"DateTime": "2016-08-15T14:00:00",
"TimeZone": "Pacific Standard Time"
},
"End": {
"DateTime": "2016-08-15T14:30:00",
"TimeZone": "Pacific Standard Time"
},
"Attendees": [
{
"EmailAddress": {
"Address": "nanyu@o365e3w15.onmicrosoft.com",
"Name": "Nan Yu"
},
"Type": "Required"
}
]
}
添加附件
POST: https://outlook.office.com/api/v2.0/me/events/{eventId}/attachments
authorization: bearer {token}
content-type: application/json
{
"@odata.type":"#Microsoft.OutlookServices.FileAttachment",
"Name":"test.txt",
"ContentBytes":"aHR0cDovL2dpb25rdW56LmdpdGh1Yi5pby9jaGFydGlzdC1qcy9leGFtcGxlcy5odG1sDQoNCmh0dHBzOi8vd3d3LnlvdXR1YmUuY29tL3dhdGNoP3Y9MGttZGpxZ085SVkNCjM2OjAyDQpodHRwczovL3d3dy55b3V0dWJlLmNvbS93YXRjaD92PW9FVHY2djlmN3djIA0KNjoxOQ0KDQpodHRwczovL2FuZ3VsYXItdWkuZ2l0aHViLmlvL2Jvb3RzdHJhcC8="
}
内容字节是文本文件的base64格式字符串,下面是使用C#将文本文件转换为base64字符串的示例:
Convert.ToBase64String(File.ReadAllBytes(@"C:\users\user1\desktop\test.txt"))
更新活动
PATCH:https://outlook.office.com/api/v2.0/me/events/{eventid}
authorization: bearer {token}
content-type: application/json
{
"Body": {
"ContentType": "HTML",
"Content": "I think it will meet our requirements!(Update attachments)"
}
}