2016-08-23 6 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) 

他の記事は、取り付けのために別々に(here)JSONのプロパティを設定することを示唆しているので、私は(「data_attachment」および「json_payloadAttachment」を参照)要求に加えて、以下data_attachmentコードを含んでいるので。しかし、私はどのように要求にそれを追加して1つの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

関連する問題