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

Python Office 365 API-创建带有附件的事件,python,office365,Python,Office365,我正在使用下面的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('Pleas

我正在使用下面的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)
但是,当我尝试包含附件时,它不起作用(请参见下面的附件代码)

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

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

任何帮助都将不胜感激。

要为活动创建附件,我们需要发送另一个请求。以下是exist事件的描述

创建附件后,我们还需要更新事件,以便其他人可以看到更改

以下是为事件创建附件的示例:

创建一个事件

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="
}
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)"
  }
}
内容字节是文本文件的base64格式字符串,下面是使用C#将文本文件转换为base64字符串的示例:

更新事件

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="
}
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)"
  }
}

谢谢你的回复。所以在我的脚本中,我需要先创建附件,然后创建事件,然后捕获事件的id,然后用创建的附件更新该事件?对不起,我迷路了,请帮助提供一些示例代码。。。。谢谢。因为我不熟悉PHP,我会在原始帖子中提供一个原始REST示例供您参考。希望对你有帮助。谢谢你的帮助。我正在使用Python,也是Python新手。我能够创建事件,捕获事件ID,但我完全无法将文件转换为base64格式以添加附件。有什么建议吗?此外,我可以附加不同的文件扩展名,如pdf、doc、jpeg和excel吗?我用我尝试过的附件代码更新了我的帖子,但没有成功。请看看我做错了什么。提前感谢。终结点“”不支持基本令牌。如果将其替换为“”,是否对您有效?要调用Office 365 REST API,我们需要使用承载令牌来注册应用程序,并使用OAuth来请求令牌。你可以参考更多细节。这有助于理解Office 365 API的身份验证。