使用 Microsoft Graph SDK 的批处理请求移动电子邮件/消息



我的目标是处理一个文件夹中的电子邮件,然后将它们移动到我有id的另一个文件夹。为了减轻工作量,我正在尝试使用批处理功能。

不幸的是,每次我尝试运行批处理函数时,都会出现一个异常,消息为Code: invalidRequest Message: Unable to deserialize content.

有问题的代码,只针对一个请求进行了简化,可以在下面找到。

var batch = new BatchRequestContent();
var json = JsonSerializer.Serialize( new { destinationId =  Graph.Me.Messages[messageId].Move(folderId).Request().RequestBody.DestinationId } );
var jsonMessage = Graph.HttpProvider.Serializer.SerializeAsJsonContent(json);
var request = Graph.Me.Messages[messageId].Move(folderId).Request().GetHttpRequestMessage();
request.Method = HttpMethod.Post;
request.Content = jsonMessage;
batch.AddBatchRequestStep(request);
var res = await Graph.Batch.Request().PostAsync(batch);

我已经将问题缩小到关于request.Content,因为如果没有它,它就会通过,尽管返回时出现了400个关于丢失身体的错误。

batch.ReadAsStringAsync()复制字符串并将其直接粘贴到图形资源管理器中,然后使用它来运行查询,结果为200。

根据我所做的尝试,我开始认为这是SDK批量的限制。有什么想法吗?

虽然这不是我想要的解决方案,但它可以作为创可贴解决方案。

基本上,在将所有步骤添加到BatchRequestContent之后,就可以使用ReadAsStringAsync()来获取请求的正文。然后使用库本身发送HTTP请求,如下所示。

HttpRequestMessage hrm = Graph.Batch.Request().GetHttpRequestMessage();
hrm.Method = HttpMethod.Post;
hrm.Content = Graph.HttpProvider.Serializer.SerializeAsJsonContent(await batch.ReadAsStringAsync());
// Authenticate (add access token) our HttpRequestMessage
await Graph.AuthenticationProvider.AuthenticateRequestAsync(hrm);
// Send the request and get the response.
HttpResponseMessage res = await Graph.HttpProvider.SendAsync(hrm);

最新更新