Office 365 API - 创建带有附件的事件

Office 365 API - Create Event With Attachment

我正在使用下面的 Python 脚本为 Office 365 创建事件并且它有效。但是,我很难(一整天都在谷歌上搜索)找出如何将附件与从下面的脚本生成的事件一起包含在内。

下面的代码适用于创建事件。

# 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)

但是当我尝试包含附件时,它不起作用(请参阅下面的附件代码)

# 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)

接下来,我尝试把进程分开。因此,首先我创建了事件并捕获了 eventID。然后尝试将 eventID 包含在 URL 中(见下文),但仍然没有成功。

import urllib2
import getpass
import os
import json
import sys
import base64
import traceback

# Set the request parameters
url = 'https://outlook.office.com/api/v1.0/me/events/AAMkADA1OWVjOTkxLTlmYmEtNDAwMS04YWU3LTNkNDE2YjU2OGI1ZABGBBBBBBD_fa49_h8OTJ5eGdjSTEF3BwBOcCSV9aNzSoXurwI4R0IgBBBBBBENAABOcCSV9aNzSoXurwI4R0IgAAHzfZ0mAAA=/attachments'
user = 'user1@domain.com'
pwd = "password123"

# Create JSON payload
data = {
        "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
        "Name": "menu.txt",
        "ContentBytes": "VGVzdCAxMjM0NQ=="
      }


print data

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).read()

任何帮助将不胜感激。

要为活动创建附件,我们需要发送另一个请求。这是存在事件的 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)"
  }
}