【发布时间】:2021-09-26 17:58:33
【问题描述】:
我需要使用patch 请求在 OneNote 页面之间复制图像。如何使用 MS Graph API 做到这一点?
【问题讨论】:
标签: typescript microsoft-graph-api onenote-api
我需要使用patch 请求在 OneNote 页面之间复制图像。如何使用 MS Graph API 做到这一点?
【问题讨论】:
标签: typescript microsoft-graph-api onenote-api
这是 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;
}
【讨论】: