i want to update an OneNote page with MS Graph but i get the following error:
{
"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"
}
}
}
My code:
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)
I've already tried:
result = requests.patch(dest, data=json.dumps(payload), headers=headers)
result = requests.patch(dest, json=json.dumps(payload), headers=headers)
result = requests.patch(dest, json=payload, headers=headers)
As https://learn.microsoft.com/en-us/graph/onenote-update-page says
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.
says that you need Array of JSON objects, so make your payload into an Array instead.
payload = [
{
'target': 'body',
'action': 'prepend',
'content': '<p>New paragraph as first child in the first div</p>'
}
]
And then try to do the request like this:
result = requests.patch(dest, json=payload, headers=headers)
json=
parameter does the json.dumps
conversion for you already