2016-08-23 117 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