Outlook Web 挂钩订阅

Outlook Web hook Subscription

我正在使用 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}'

要post为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)

您可以使用:

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