首页 > 解决方案 > Outlook Web 挂钩订阅

问题描述

我正在使用 Outlook Webhook 订阅并在 QA 服务器上工作。

根据 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}'

标签: python-3.xmicrosoft-graph-apioutlook-restapi

解决方案


要发布为 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)

推荐阅读