【问题标题】:Trying to update a OneNote page using Python, requests and MS Graph API results in "Unknown error"尝试使用 Python、请求和 MS Graph API 更新 OneNote 页面会导致“未知错误”
【发布时间】:2020-09-05 00:37:45
【问题描述】:

不知道我在这里做错了什么 我正在尝试使用 Python 脚本来更新我可以正常检索的 OneNote 页面 我正在使用设备身份验证流程,并且在获取令牌时发送的范围是这些

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

我可以使用这个阅读页面

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)

现在我正在使用上述确认为可访问的链接来更新同一页面

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)

我收到了这个错误

{
  "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('myname@email.com')/notes'.\"\r\n}",
    "innerError": {
      "request-id": "334a17e2-09f2-4744-900b-29b325dd1e64",
      "date": "2020-05-19T03:08:51"
    }
  }
}

更新:尝试创建新页面会导致相同类型的错误 更新 2:这里有趣的是,当我尝试使用 MS Graph API 时,回复回来了关于 OneNote API。 我去了 Portal.azure.com,我给 MS Graph 写了更多的信息(它只读,上面提到的其他权限已应用于 OneNote。)这并没有改变任何东西 我将稍等片刻,看看这是否是权限传播错误并解决了问题。 如果我仍然看到 OneNote 消息表明这里有问题,应该是 MS Graph 回复我

Update2:以下是 OneNote 和 MSGraph 的权限(范围)

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

但我仍然收到关于 OneNote 无法找到 URI 的相同错误消息

更新 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)

上面仍然给我同样的错误。很抱歉最初的粘贴,这是由于多次尝试找到一种方法来完成这项工作。我知道那个页面,我试过了

{
  "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('my@email.com')/notes'.\"\r\n}",
    "innerError": {
      "request-id": "bcc11791-d847-46af-b6ee-e3fd060c4ca0",
      "date": "2020-05-20T01:01:50"
    }
  }
}

不确定这是否相关,但当我尝试使用 MS Graph Explorer 时,我无法使用“创建页面”示例。该示例应该加载我应该替换 ID 的模板......并添加页面的内容。没有发生!

【问题讨论】:

    标签: microsoft-graph-api onenote-api microsoft-graph-onenote


    【解决方案1】:

    这完全是我的愚蠢案例 我在定义

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

    但我在打电话 result=requests.patch(endpoint,headers=headers,json=data)

    端点具有此值 https://www.onenote.com/api/v1.0/users('my@email.com')/notes'

    所以上面的代码工作得很好,但在我发布的问题中使用 content_end_point 而不是端点。该死的,我在这上面浪费了这么多时间。复制和粘贴时会发生这种情况

    【讨论】:

      猜你喜欢
      • 2022-10-07
      • 1970-01-01
      • 2021-05-20
      • 2021-09-26
      • 2021-02-28
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多