尝试通过 API 创建带有渲染 PDF 的 OneNote 页面时出现不一致的行为

问题描述

我一直在尝试在 OneNote 中创建一个新页面来显示渲染的 PDF 页面,我的代码如下

$n = [System.Guid]::NewGuid().ToString(); 
 $content = @" 
 <!DOCTYPE html>
 <html>
 <head>
 <title>$n</title>
 </head>
 <body>
 <img data-render-src =`"name:$n`" />
 </body>
 </html>
 "@  
 $fileBytes = [System.IO.File]::ReadAllBytes($FilePath);
 $fileEnc = [System.Text.Encoding]::GetEncoding('ISO-8859-1').GetString($fileBytes);
 $boundary = [System.Guid]::NewGuid().ToString(); 
 $LF = "`r`n";
 $bodyLines = ( 
     "--$boundary`r`nContent-Disposition: form-data; name=`"Presentation`"","Content-Type: text/html$LF",$content,"--$boundary`r`nContent-Disposition: form-data; name=`"$n`"","Content-Type: application/pdf$LF",$fileEnc,"--$boundary--$LF" 
 ) -join $LF
    
 $r = Invoke-RestMethod -Headers @{Authorization = "Bearer $($myAccessToken.AccessToken)"; Boundary = $boundary } `
     -Uri "https://graph.microsoft.com/v1.0/groups/$group/onenote/sections/$sectionId/pages" `
     -Method POST `
      -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines

代码在某些时候运行良好。没有理由或押韵,有时页面会像我想要的那样使用渲染的 pdf 创建。其他时候,页面将使用文件附件创建。该文件是正确的,可以从一个笔记下载并打开。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)