带有 python 请求的补丁页抛出 "request's JSON invalid" - MS OneNote API

Patch page with python requests throws "request's JSON invalid" - MS OneNote API

我想用 MS Graph 更新 OneNote 页面,但出现以下错误:

{
  "error": {
    "code": "20109",
    "message": "The request's JSON was invalid or could not be parsed.",
    "innerError": {
      "date": "2020-11-17T07:22:13",
      "request-id": "a5d97ae9-d792-4e7e-8b98-3ff450916905",
      "client-request-id": "a5d97ae9-d792-4e7e-8b98-3ff450916905"
    }
  }
}

我的代码:

dest = f'https://graph.microsoft.com/v1.0/me/onenote/pages/{page_id}/content'
payload = {
    'target': 'body',
    'action': 'prepend',
    'content': '<p>New paragraph as first child in the first div</p>'
}
print(page_id)
print(json.dumps(payload))
print(app.session.get(app.ACCESS_TOKEN))
headers = {'Authorization': 'Bearer ' + app.session.get(app.ACCESS_TOKEN), 'Content-Type': 'application/json'}
result = requests.patch(dest, json.dumps(payload), headers=headers)
print(result.text)

我已经试过了:

https://docs.microsoft.com/en-us/graph/onenote-update-page所说

Your changes are sent in the message body as an array of JSON change objects. Each object specifies the target element, new HTML content, and what to do with the content.

说您需要 JSON 个对象的数组,因此请将您的有效载荷改为数组。

payload = [
    {
        'target': 'body',
        'action': 'prepend',
        'content': '<p>New paragraph as first child in the first div</p>'
    }
]

然后尝试这样请求:

result = requests.patch(dest, json=payload, headers=headers)

json= 参数已经为您完成 json.dumps 转换