我的目标是能够使用 C# 以编程方式更新 OneNote 页面数据。Microsoft Graph API 参考文档建议这只能通过页面元素而不是页面来完成,并提供了以下 C# Graph SDK 示例:
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(@"[
{
'target':'#para-id',
'action':'insert',
'position':'before',
'content':'<img src=""image-url-or-part-name"" alt=""image-alt-text"" />'
},
{
'target':'#list-id',
'action':'append',
'content':'<li>new-page-content</li>'
}
]
"));
var pages = new OnenotePage();
pages.Content = stream;
await graphClient.Me.Onenote.Pages["{onenotePage-id}"]
.Request()
.UpdateAsync(pages);
以下是我的代码的相关片段:
GraphServiceClient client; // authenticated service client
CancellationToken cancellationToken; // a cancellation token
string userId; // identifier of user whose page contains the paragraph to be updated
string pageId; // identifier of page containing paragraph to be updated
string paragraphId; // identifier of paragraph to be updated
string filePath; // location of text file containing updated paragraph data
await client.Users[userId].Onenote.Pages[pageId]
.Request()
.UpdateAsync(new OnenotePage
{
Content = new MemoryStream(Encoding.UTF8.GetBytes(
// [
// {
// 'target':'{paragraphId}',
// 'action':'replace',
// 'content':'<p>{File.ReadAllText(filePath)}</p>'
// }
// ]
$"[{{'target':'{paragraphId}','action':'replace','content':'<p>{File.ReadAllText(filePath)}</p>'}}]"))
}, cancellationToken);
Microsoft 的 REST 文档包含PATCH /users/{id | userPrincipalName}/onenote/pages/{id}/content
一个有效的 HTTP 请求,所以我上面的代码似乎应该可以工作,即使它没有使用.Me
他们示例中的选项。但是,出于某种原因,我的代码在尝试执行命令时不断抛出一个InvalidOperationException
,并声明它。以下是异常的详细信息:"Timeouts are not supported on this stream,"
await
System.InvalidOperationException
HResult=0x80131509
Message=Timeouts are not supported on this stream.
Source=System.Private.CoreLib
StackTrace:
at System.IO.Stream.get_ReadTimeout()
当我尝试在官方 Graph Explorer 上运行原始 REST 命令时,我收到一条No Content - 204
消息,确认 PATCH 有效。但是,请再次注意,我只是使用 C# MS Graph SDK。
我哪里错了?我怎样才能实现我的目标?
编辑:我仍然没有解决 SDK 向InvalidOperationException
我抛出 s 的问题,因此不认为这件事已解决,但由于 API 似乎工作得很好,我继续并找到了一种解决方法来实现我的目标。发布在这里,以防其他人遇到我同样的问题并需要一些有用的东西。
GraphServiceClient client; // authenticated service client
CancellationToken cancellationToken; // a cancellation token
string userId; // identifier of user whose page contains the paragraph to be updated
string pageId; // identifier of page containing paragraph to be updated
string paragraphId; // identifier of paragraph to be updated
string filePath; // location of text file containing updated paragraph data
HttpRequestMessage request = new HttpRequestMessage(
HttpMethod.Patch,
client.Users[userId].Onenote.Pages[pageId].Content
.Request()
.RequestUrl)
{
Content = new StringContent(
// [
// {
// 'target':'{paragraphId}',
// 'action':'replace',
// 'content':'<p>{File.ReadAllText(filePath)}</p>'
// }
// ]
$"[{{'target':'{paragraphId}','action':'replace','content':'<p>{File.ReadAllText(filePath)}</p>'}}]",
Encoding.UTF8,
"application/json")
};
await client.AuthenticationProvider.AuthenticateRequestAsync(request);
await client.HttpProvider.SendAsync(request);