Search code examples
microsoft-graph-apionenote-apimicrosoft-graph-onenote

Trying to update a OneNote page using Python, requests and MS Graph API results in "Unknown error"


Not sure what I am doing wrong here I am trying to use a Python script to update a OneNote page that I can retrieve just fine I am using a device authentication flow and the scopes send when I am getting the token are these

scopes=[
        "Notes.Read",
        "Notes.Read.All",
        "Notes.ReadWrite",
        ]

I am able to read the page using this

headers={'Authorization': 'Bearer ' + token}
content_end_point='https://graph.microsoft.com/v1.0/users/{my-user-id}/onenote/pages/{page-id}/content?includeIDs=true'
content=requests.get(content_end_point,headers=headers)
print(content.text)

Now I am using the above confirmed as accessible link to update the very same page

headers={'Authorization': 'Bearer ' + token,'Content-Type': 'application/json' }
content_end_point='https://graph.microsoft.com/v1.0/me/onenote/notebooks/pages/{page-id}/content'

data= {
    'target':'body',
    'action':'prepend',
    'content':'<p data-id="first-child">New paragraph as first child in the first div</p>'
  }

result=requests.patch(endpoint,headers=headers,json=data)
print(result.text)

and I am getting this error

{
  "error": {
    "code": "UnknownError",
    "message": "{\r\n  \"Message\": \"No HTTP resource was found that matches the request URI 'https://www.onenote.com/api/v1.0/users('[email protected]')/notes'.\"\r\n}",
    "innerError": {
      "request-id": "334a17e2-09f2-4744-900b-29b325dd1e64",
      "date": "2020-05-19T03:08:51"
    }
  }
}

Update: trying to create a new page results in the same type of error Update2: what is interesting here is that the reply comes back about OneNote APIs while I am trying to use MS Graph API. I went to Portal.azure.com and I gave more writes to MS Graph (it had read only and the other permissions above mentioned were applied to OneNote.) That did not change anything I am going to wait a little bit to see if this is a permission propagation error and that fixed the problem. If I am still seeing the OneNote message that means there is something wrong here, it should be MS Graph getting back to me

Update2:The below are the permissions(scopes) for both, OneNote and MSGraph

scopes=[
        "Notes.Read",
        "Notes.Read.All",
        "Notes.ReadWrite",
        "Notes.ReadWrite.All", 
        "Notes.Create",
        "Notes.ReadWrite.CreatedByApp"
        ]

But I am still receiving the same error message about OneNote not being able to find the URI

Update 3

headers={'Authorization': 'Bearer ' + token,'Content-Type': 'application/json' }
content_end_point='https://graph.microsoft.com/v1.0/me/onenote/pages/{page-id}/content'

data= {
    'target':'body',
    'action':'prepend',
    'content':'<p data-id="first-child">New paragraph as first child in the first div</p>'
  }


result=requests.patch(endpoint,headers=headers,json=data)
print(result.text)

The above still gives me the same error. Sorry for the initial paste, it was due to multiple attempts to find a way to make this work. I knew about that page and I tried

{
  "error": {
    "code": "UnknownError",
    "message": "{\r\n  \"Message\": \"No HTTP resource was found that matches the request URI 'https://www.onenote.com/api/v1.0/users('[email protected]')/notes'.\"\r\n}",
    "innerError": {
      "request-id": "bcc11791-d847-46af-b6ee-e3fd060c4ca0",
      "date": "2020-05-20T01:01:50"
    }
  }
}

Not sure if this is related but when I try to use MS Graph Explorer I can't use the "Create a page" example. That example is supposed to load a template where I am supposed to replace an ID....and add the content of the page. Not happening!


Solution

  • This a case of pure stupidity at my end I am defining

    content_end_point='https://graph.microsoft.com/v1.0/me/onenote/pages/{page-id}/content'

    but I am calling result=requests.patch(endpoint,headers=headers,json=data)

    with endpoint having this value https://www.onenote.com/api/v1.0/users('[email protected]')/notes'

    So the above code works just fine but instead of endpoint use content_end_point in my posted question. Damn it I wasted so much time on this. That happens when you copy and paste