gpt4 book ai didi

c# - 微软图: Invalid batch payload format

转载 作者:行者123 更新时间:2023-12-02 07:57:59 30 4
gpt4 key购买 nike

我正在尝试批处理update用户列表,使用 example from the docs 之后的 MS Graph Batch API但收到错误“消息:无效的批处理负载格式。”

// batch disable all users
var allUsers = await GraphHelper.client.Users.Request().Select("id").GetAsync();

var batchRequestContent = new BatchRequestContent();
allUsers.Select(user =>
{
var httpRequest = GraphHelper.client.Users[user.Id].Request().GetHttpRequestMessage();
httpRequest.Method = HttpMethod.Put;
httpRequest.Content = GraphHelper.client.HttpProvider.Serializer.SerializeAsJsonContent(new User { AccountEnabled = false });
return batchRequestContent.AddBatchRequestStep(httpRequest);
});
var userUpdateResult = await GraphHelper.client.Batch.Request().PostAsync(batchRequestContent);

下面用于禁用用户的代码无需批处理即可运行。

var user = new User { AccountEnabled = false };
var userResult = await GraphHelper.client.Users[id].Request().UpdateAsync(user);

最佳答案

如果我使用 lambda 运算符,而不是将其更改为 foreach 循环并添加批处理请求,我也能得到相同类型的错误,如下所示。

static async Task<BatchResponseContent> BulkUpdate()
{
var allUsers = await GraphHelper.graphClient.Users.Request().Top(10).Select("id").GetAsync();

var batchRequestContent = new BatchRequestContent();
foreach(var items in allUsers)
{
var httpRequest = GraphHelper.graphClient.Users[items.Id].Request().GetHttpRequestMessage();
httpRequest.Method = HttpMethod.Patch;
httpRequest.Content = GraphHelper.graphClient.HttpProvider.Serializer.SerializeAsJsonContent(new User { AccountEnabled = false });
batchRequestContent.AddBatchRequestStep(httpRequest);
}
var userUpdateResult = await GraphHelper.graphClient.Batch.Request().PostAsync(batchRequestContent);
return userUpdateResult;
}

这样我就可以将批量请求添加到batchRequestContent变量中,如下图所示。

(JSON Data)Body of Batch Request

注意:JSON 批量请求当前为 limited最多 20 个单独请​​求。

关于c# - 微软图: Invalid batch payload format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62370545/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com