首页 > 解决方案 > Microsoft graph:找不到指定的 PATCH 目标 $value 和与指定 PATCH 目标相关的页面内容

问题描述

我在更新 OneNote 页面内容时遇到了这个错误。但是,在向您展示问题之前,让我先解释一下我为 OneNote 输入的 HTML。

这是我的输入 HTML 模板:

<!DOCTYPE html>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <object data-id="markdown-file" data-attachment="markdown.md" data="name:markdown" type="text/markdown" />
        <div data-id="content">{{ content goes here }}</div>
    </body>
</html>

如果div[data-id="content"]存在,我发送了以下补丁命令来更新内容:

{
    'target': generated id of div[data-id="content"],
    'action': 'replace',
    'content': '<div data-id="content">{{ actual content }}</div>'
}

否则,我使用另一个命令:

{
    'target': 'body',
    'action': 'append',
    'content': '<div data-id="content">{{ actual content }}</div>'
}

大多数时候,它工作正常。但有时不是。假设我们有以下输出 html: HTML

<html lang="en-US">
    <head>
        <title>2</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
        <div id="div:{bbed3bc8-6ec5-4900-b1ee-a11259b4d796}{2}" data-id="_default" style="position:absolute;left:48px;top:120px;width:624px">
            <object data-attachment="markdown.md" type="text/markdown" data="https://graph.microsoft.com/v1.0/users('195d63c8-4d1e-4073-b535-5d8a32b6f6ce')/onenote/resources/1-5ae390556d1e4351b358b6e1a667a226!1-051437c2-f608-445d-b537-e68aea2dfcd9/$value" data-id="markdown-file" />
            <div data-id="content" id="div:{ce16905b-a76b-4e35-86de-1a46b5f8a62f}{69}:{ce16905b-a76b-4e35-86de-1a46b5f8a62f}{81}">
                <table id="table:{ce16905b-a76b-4e35-86de-1a46b5f8a62f}{69}" style="border:1px solid;border-collapse:collapse">
                    <tr id="tr:{ce16905b-a76b-4e35-86de-1a46b5f8a62f}{70}">
                        <td id="td:{ce16905b-a76b-4e35-86de-1a46b5f8a62f}{72}" style="background-color:white;border:1px solid;text-align:center"><span style="font-family:BlinkMacSystemFont;color:#363636;font-weight:bold">Head</span></td>
                    </tr>
                    <tr id="tr:{ce16905b-a76b-4e35-86de-1a46b5f8a62f}{71}">
                        <td id="td:{ce16905b-a76b-4e35-86de-1a46b5f8a62f}{75}" style="background-color:white;border:1px solid"><span style="font-family:BlinkMacSystemFont;color:#363636">Column</span></td>
                    </tr>
                </table>
            </div>
        </div>
    </body>
</html>

注意div[data-id="content"] 包含一个表。如果我尝试更换div[data-id="content"],则会显示错误The PATCH target $value specified and page content related to the specified PATCH target cannot be located。错误信息不是很清楚,所以我不知道缺少哪个目标。

但如果输出的 HTML 不仅包含表格,还包含其他元素,则可以成功替换。我的代码适用于以下输出 HTML:

<html lang="en-US">
    <head>
        <title>3</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
        <div id="div:{a61d7d65-215f-4936-a8c3-4dda9a805827}{249}" data-id="_default" style="position:absolute;left:48px;top:120px;width:624px">
            <object data-attachment="markdown.md" type="text/markdown" data="https://graph.microsoft.com/v1.0/users('195d63c8-4d1e-4073-b535-5d8a32b6f6ce')/onenote/resources/1-c52b2dd5a8d74a89a0e038373b52b3f1!1-051437c2-f608-445d-b537-e68aea2dfcd9/$value" data-id="markdown-file" />
            <div data-id="content" id="div:{25489f27-57fa-4798-b4ef-d229a5c5841f}{171}:{25489f27-57fa-4798-b4ef-d229a5c5841f}{187}">
                <table id="table:{25489f27-57fa-4798-b4ef-d229a5c5841f}{171}" style="border:1px solid;border-collapse:collapse">
                    <tr id="tr:{25489f27-57fa-4798-b4ef-d229a5c5841f}{172}">
                        <td id="td:{25489f27-57fa-4798-b4ef-d229a5c5841f}{174}" style="background-color:white;border:1px solid;text-align:center"><span style="font-family:BlinkMacSystemFont;color:#363636;font-weight:bold">Head</span></td>
                    </tr>
                    <tr id="tr:{25489f27-57fa-4798-b4ef-d229a5c5841f}{173}">
                        <td id="td:{25489f27-57fa-4798-b4ef-d229a5c5841f}{177}" style="background-color:white;border:1px solid"><span style="font-family:BlinkMacSystemFont;color:#363636">Column</span></td>
                    </tr>
                </table>
                <p id="p:{25489f27-57fa-4798-b4ef-d229a5c5841f}{187}" style="margin-top:5.5pt;margin-bottom:5.5pt"><span style="font-family:BlinkMacSystemFont;color:#4a4a4a">hello</span></p>
            </div>
        </div>
    </body>
</html>

两个输出 html 的唯一区别是第二个有 ap 标签。这个问题看起来很奇怪。

这是我更新页面内容的代码:

def update_page(id):
    original_content = _get_page_content(id)
    original_document = PyQuery(original_content)
    content_div = original_document('div[data-id="content"]')
    page = request.json
    new_document = PyQuery(page['content'])

    commands = [
        {
            'target': 'title',
            'action': 'replace',
            'content': page['title']
        },
        {
            'target': '#markdown-file',
            'action': 'replace',
            'content': MARKDOWN_FILE_OBJECT_HTML
        }
    ]

    content = '<div data-id="content">{0}</div>'.format(
        OneNoteHtmlMapper(new_document).get_html()) # OneNoteHtmlMapper is not implemented, it simply calls new_document.outer_html()

    if content_div:
        commands.append({
            'target': content_div.attr('id'),
            'action': 'replace',
            'content': content
        })
    else:
        commands.append({
            'target': 'body',
            'action': 'append',
            'content': content
        })

    files = {
        'Commands': ('', io.StringIO(json.dumps(commands)),
                     'application/json'),
        'markdown': ('markdown.md', io.StringIO(page['markdown']),
                     'text/markdown')
    }

    oauth_client = oauth.microsoft_graph
    response = oauth_client.request(
        'PATCH', 'me/onenote/pages/{0}/content'.format(id), files=files)

    return response.content, response.status_code

提前致谢!

标签: microsoft-graph-api

解决方案


临时修复:如果 div 仅包含表格,则将 1px * 1px 白色图像附加到 div。


推荐阅读