2016-08-23 113 views
0

我对Python非常陌生。我遇到了这个问题,希望你能帮上忙。让我解释一下我试图做的事情,让我知道我是否让你困惑。Json在请求中添加附加有效载荷

我有这个Python脚本,它可以很好地创建一个事件。

# Set the request parameters 
url = 'https://outlook.office365.com/api/v1.0/me/events?$Select=Start,End' 
user = '[email protected]' 

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": "[email protected]", 
     "Name": "User1" 
     }, 
     "Type": "Required" }, 

     { 
     "EmailAddress": { 
     "Address": "[email protected]", 
     "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”和“json_payloadAttachment”)。但是,我不确定如何在请求中添加该消息并创建一个POST。

# Set the request parameters 
url = 'https://outlook.office365.com/api/v1.0/me/events?$Select=Start,End' 
user = '[email protected]' 

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": "[email protected]", 
     "Name": "User1" 
     }, 
     "Type": "Required" }, 

     { 
     "EmailAddress": { 
     "Address": "[email protected]", 
     "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) 

请帮忙。提前致谢。

回答

1

看来您需要合并数据;例如,您可以在数据字典中添加另一个键,名为Attachments,其中包含一组字典并以这种方式合并它们;然后将您的数据序列化为JSON。 你不需要json_payloadAttachment

... 
data["Attachments"] = [data_attachment] 
json_payload = json.dumps(data) 

你也可以根据您发布的链接缺少HasAttachments关键。

data["HasAttachments"] = True 
+0

感谢您的帮助。我尝试了你的建议,但没有运气(也没有错误)。也许从我发布的链接的建议不清楚。如果你有一个时刻,请阅读该链接中的回复,其中说:“我得到它的工作....如果文档实际上列出这是必须设置的属性之一,这将是很好的。”请看看你能否理解并提供建议。我非常感谢你的帮助。 – Milacay