代码之家  ›  专栏  ›  技术社区  ›  Freewalker

如何使用MS Graph API将OneNote图像复制到另一个OneNote页面?

  •  0
  • Freewalker  · 技术社区  · 5 年前

    我需要使用 patch 要求如何使用MS Graph API实现这一点?

    1 回复  |  直到 5 年前
        1
  •  0
  •   Freewalker    5 年前

    下面是TypeScript的工作实现。图像只是嵌入到HTML中(不是微软有文档记录的插入图像的方法,但效果很好)。

    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;
    }
    
    
    推荐文章