【问题标题】:How can I copy a OneNote image to another OneNote page using the MS Graph API?如何使用 MS Graph API 将 OneNote 图像复制到另一个 OneNote 页面?
【发布时间】:2021-09-26 17:58:33
【问题描述】:

我需要使用patch 请求在 OneNote 页面之间复制图像。如何使用 MS Graph API 做到这一点?

【问题讨论】:

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


    【解决方案1】:

    这是 TypeScript 中的工作实现。该图像只是嵌入在 HTML 中(不是 Microsoft 记录的插入图像的方式,但它工作正常)。

    const resourceUrl = "https://graph.microsoft.com/v1.0/users('someone@test.com')/onenote/resources/{resourceId}/$value";
    const imageData = await downloadImage(client, resourceUrl);
    const b64 = Buffer.from(imageData).toString("base64");
    const htmlString = `<p>test image:</p><img width="30" src="data:image/jpeg;base64,${b64}" />`;
    const patchData = {
      target: "body",
      action: "prepend",
      content: htmlString,
    };
    await client
      .api(`/me/onenote/pages/${testInsertPageId}/content`)
      .patch([patchData]);
    
    export async function downloadImage(
      client: Client,
      imgSrc: string,
    ): Promise<Uint8Array> {
      const result: ReadableStream = await client.api(imgSrc).get();
      const reader = result.getReader();
    
      let data: Uint8Array = new Uint8Array();
      let readResult = await reader.read();
      while (!readResult.done) {
        const value: Uint8Array = readResult.value;
        const prevData = data;
        data = new Uint8Array(data.length + value.length);
        data.set(prevData);
        data.set(value, prevData.length);
    
        readResult = await reader.read();
      }
    
      return data;
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      • 2020-07-11
      • 2021-05-20
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多