azure 使用MS graph API v5批量更新用户

xmd2e60i  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(138)

我正在使用Microsoft Graph API与我的ADB2C应用程序进行交互。我正在代码中单独提取、创建、更新和删除用户,一切正常。
现在有一个特定的要求更新某些字段的几个用户,并提供它作为一个功能,以管理员用户.我使用下面的代码作为例子,我从这个link和这个link批量更新adb2c中的用户.

  1. string filterQuery = string.Join(" or ", emails.Select(email => $"mail eq '{email}'"));
  2. var users = new List<User>();
  3. var batchRequestContent = new BatchRequestContentCollection(graphClient);
  4. var result = await graphClient.Users.GetAsync((requestConfiguration) =>
  5. {
  6. requestConfiguration.QueryParameters.Filter = filterQuery;
  7. });
  8. if (result != null && result.Value.Count > 0)
  9. {
  10. users.AddRange(result.Value);
  11. }
  12. foreach (var user in users)
  13. {
  14. user.AccountEnabled = !value;
  15. var userRequest = graphClient.Users.ToPostRequestInformation(user);
  16. await batchRequestContent.AddBatchRequestStepAsync(userRequest);
  17. }
  18. var response = await graphClient.Batch.PostAsync(batchRequestContent);
  19. var responses = await response.GetResponsesStatusCodesAsync();

字符串
现在,由于这是一个发布请求,因此它会失败,并出现如下异常
必须指定密码才能创建新用户。
我更改了userRequest.HttpMethod = Method.PATCH;,使其行为类似于补丁(更新),但发生了以下异常。
请求目标不允许使用指定的HTTP方法。
所以问题是如何使用上面的参考示例来批量更新使用ms graph API v5的用户
谢谢.

7lrncoxx

7lrncoxx1#

  1. var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
  2. var requestBody = new User
  3. {
  4. OfficeLocation = "22/5555",
  5. };
  6. var requestBody2 = new User
  7. {
  8. OfficeLocation = "18/6666",
  9. };
  10. var req1 = graphClient.Users["user_id1"].ToPatchRequestInformation(requestBody);
  11. var req2 = graphClient.Users["user_id2"].ToPatchRequestInformation(requestBody2);
  12. var batchRequestContent = new BatchRequestContent(graphClient);
  13. var userRequestId = await batchRequestContent.AddBatchRequestStepAsync(req1);
  14. var userRequestId2 = await batchRequestContent.AddBatchRequestStepAsync(req2);
  15. var returnedResponse = await graphClient.Batch.PostAsync(batchRequestContent);

字符串
代码为我工作。
请允许我指出一些问题。首先,对于更新用户图API,我们只有端点选项
PATCH /users/{id| userPrincipalName}
这意味着使用SDK,我们只能为特定用户await graphClient.Users["userId"].PatchAsync(requestBody)更新用户配置文件,但不能使用代码var userRequest = graphClient.Users.ToPostRequestInformation(user);。您的代码看起来像是将整个用户模型添加到请求中,并让API自动检测谁需要更新,但这是不可行的。
第二个问题是,我们试图更新一个用户,所以我们需要得到的请求不是ToPostRequestInformation,而是ToPatchRequestInformation
第三个是我自己的问题,一开始我只为两个请求使用了requestBody,但在执行代码后,我发现user2没有更新,在我为user 2创建requestBody2后,它工作了。
第四个是我自己的问题,以及,在第一次当我试图更新businessPhones像什么样的演示显示,它失败了,我在 Postman 测试,我得到了错误消息与Insufficient privileges,这是因为我试图更新管理员用户配置文件,这需要其他权限,这一节提到了它.

展开查看全部

相关问题