【问题标题】:What is the "best practice" when adding a user to a group using Microsoft Graph API?使用 Microsoft Graph API 将用户添加到组时的“最佳实践”是什么?
【发布时间】:2022-10-24 16:52:03
【问题描述】:

在尝试使用 Graph API 将用户添加到组之前,是否应该先检查用户是否已经是成员?或者我应该尝试添加和处理假设失败意味着它们已经存在?

如果我使用以下 GET 请求来检查用户是否在组中,我会收到 404 响应。

https://graph.microsoft.com/v1.0/groups/{{groupId}}/members/{userId}}

回复:

  "error": {
    "code": "Request_ResourceNotFound",
    "message": "Resource '{{groupId}}' does not exist or one of its queried reference-property objects are not present.",
    "innerError": {
      "date": "2022-10-22T07:44:27",
      "request-id": "...",
      "client-request-id": "..."
    }
  }
}```

The most likely explanation for this error is that the user doesn't exist. However I'm guessing there may also be other reasons.

If I try to add a user to a group, but they already exist, I get a 400 response:

```{
  "error": {
    "code": "Request_BadRequest",
    "message": "One or more added object references already exist for the following modified properties: 'owners'.",
    "innerError": {
      "date": "2022-10-22T07:44:56",
      "request-id": "...",
      "client-request-id": "..."
    }
  }
}```

It seems more efficient to try adding rather than first checking. Also fewer graph calls means less risk of throttling.

【问题讨论】:

    标签: microsoft-graph-api


    【解决方案1】:

    如果您使用该 API 未找到资源意味着该用户在该组中不存在,因此请尝试使用以下查询将用户添加到该组。

    POST /groups/{group-id}/members/$ref
    

    请参考本文档:https://learn.microsoft.com/en-us/graph/api/group-post-members?view=graph-rest-1.0&tabs=http

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 2021-05-12
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多