Outlook Web挂钩订阅

时间:2018-12-10 08:53:20

标签: python-3.x microsoft-graph outlook-restapi

我正在使用Outlook Webhook订阅并在质量检查服务器上工作。

根据Microsoft Graph文档,我们需要发送请求以获取webhook通知。我正在为此使用Python 3请求模块。

我正在发送以下数据,但出现错误。我无法弄清楚在此过程中哪里出了问题。

url="https://graph.microsoft.com/v1.0/subscriptions"
header={
    'Content-Type': 'application/json',
    'Authorization':"Bearer "+ "valid access token"
}

data={
    "changeType": "created,updated",
    "notificationUrl": "https://qa.example.com/get_webhook",
    "resource": "/me/mailfolders('inbox')/messages",
    "expirationDateTime": "2018-12-11T11:00:00.0000000Z"
}

response=requests.post(url, headers=header, data=data)

执行完上述行后,我得到以下<400>响应

'{\r\n  "error": {\r\n    "code": "BadRequest",\r\n    "message": 
 "Unable to read JSON request payload. Please ensure Content-Type 
 header is set and payload is of valid JSON format.",\r\n    
 "innerError": {\r\n      "request-id": "3a15ba2f-a055-4f33-a3f8- 
 f1f40cdb2d64",\r\n      "date": "2018-12-10T06:51:32"\r\n    }\r\n  
 }\r\n}'

2 个答案:

答案 0 :(得分:3)

要以JSON形式发布,您需要json属性而不是data属性(即json={"key": "value"}

url="https://graph.microsoft.com/v1.0/subscriptions"
header={
    'Content-Type': 'application/json',
    'Authorization':"Bearer "+ "valid access token"
}

data={
    "changeType": "created,updated",
    "notificationUrl": "https://qa.example.com/get_webhook",
    "resource": "/me/mailfolders('inbox')/messages",
    "expirationDateTime": "2018-12-11T11:00:00.0000000Z"
}

response=requests.post(url, headers=header, json=data)

答案 1 :(得分:1)

您可以使用:

import json
response=requests.post(url, headers=header, data=json.dumps(data))