命名空间:microsoft.graph
重要
Microsoft Graph /beta 版本下的 API 可能会发生更改。 不支持在生产应用程序中使用这些 API。 若要确定 API 是否在 v1.0 中可用,请使用 版本 选择器。
将成员添加到安全组或 Microsoft 365 组。 使用 API 在一个请求中添加多个成员时,最多只能添加 20 个成员。
注意
对于最近创建的组,此请求可能具有复制延迟。 创建组时,对象可能需要一小段时间才能跨 Microsoft Entra ID 目录副本完全复制。 在此时段内,请求向组中添加成员可能会返回错误 400 Bad Request ,并显示消息:“ 源资源对象或引用的对象之一不存在。”
要缓解此行为:
- 短暂延迟后重试 - 等待几秒钟,然后重试请求。 延迟通常是短暂的。
有关详细信息,请参阅 设计 Microsoft Entra 的最终一致性。
下表显示了可添加到安全组或 Microsoft 365 组的成员类型。
| 对象类型 |
安全组成员 |
Microsoft 365 组成员 |
| User |
|
|
| 安全组 |
|
|
| Microsoft 365 组 |
|
|
| 设备 |
|
|
| 服务主体 |
|
|
| 组织联系人 |
|
|
此 API 可用于以下国家级云部署。
| 全局服务 |
美国政府 L4 |
美国政府 L5 (DOD) |
由世纪互联运营的中国 |
| ✅ |
✅ |
✅ |
✅ |
权限
下表显示了调用此 API 时每种资源类型所需的最低特权权限。 若要了解详细信息,包括如何选择权限的信息,请参阅权限。
| 支持的资源 |
委派(工作或学校帐户) |
委派(个人 Microsoft 帐户) |
应用程序 |
|
设备 |
GroupMember.ReadWrite.All 和 Device.Read.All |
不支持。 |
GroupMember.ReadWrite.All 和 Device.ReadWrite.All |
|
组 |
GroupMember.ReadWrite.All |
不支持。 |
GroupMember.ReadWrite.All |
|
orgContact |
GroupMember.ReadWrite.All 和 OrgContact.Read.All |
不支持。 |
GroupMember.ReadWrite.All 和 OrgContact.Read.All |
|
servicePrincipal |
GroupMember.ReadWrite.All 和 Application.ReadWrite.All |
不支持。 |
GroupMember.ReadWrite.All 和 Application.ReadWrite.All |
|
user |
GroupMember.ReadWrite.All |
不支持。 |
GroupMember.ReadWrite.All |
重要
在委派方案中,还必须为登录用户分配受支持的 Microsoft Entra 角色或具有角色权限的microsoft.directory/groups/members/update自定义角色。 以下角色是此操作支持的最小特权角色,但可分配角色的组除外:
- 组所有者
- 目录作者
- 组管理员
- Identity Governance Administrator
- 用户管理员
- Exchange 管理员 - 仅适用于 Microsoft 365 组
- SharePoint 管理员 - 仅适用于 Microsoft 365 组
- Teams 管理员 - 仅适用于 Microsoft 365 组
- Yammer 管理员 - 仅适用于 Microsoft 365 组
- Intune 管理员 - 仅适用于安全组
若要将成员添加到可分配角色的组,还必须为应用分配 RoleManagement.ReadWrite.Directory 权限,并且必须为调用用户分配受支持的 Microsoft Entra 角色。
特权角色管理员 是此操作支持的最小特权角色。
HTTP 请求
POST /groups/{group-id}/members/$ref
PATCH /groups/{group-id}
| 名称 |
说明 |
| Authorization |
持有者 {token}。 必填。 详细了解 身份验证和授权。 |
| Content-type |
application/json. 必需。 |
请求正文
使用 POST /groups/{group-id}/members/$ref 语法时,提供包含 @odata.id 属性的 JSON 对象,并通过 ID 引用受支持的组成员对象类型。
使用 PATCH /groups/{group-id} 语法时,提供一个 JSON 对象,该对象包含一个 members@odata.bind 属性,该属性通过 ID 对受支持的组成员对象类型进行一个或多个引用。 即:
- 对于 Microsoft 365 组,仅
https://graph.microsoft.com/v1.0/directoryObjects/{id} 允许 且 https://graph.microsoft.com/v1.0/groups/{id} 必须是用户,因为 {id} 只有用户可以成为 Microsoft 365 组的成员。
- 对于安全组,允许以下 ID 引用:
-
https://graph.microsoft.com/v1.0/directoryObjects/{id} where {id} must 属于用户、安全组、设备、服务主体或组织联系人。
-
https://graph.microsoft.com/v1.0/groups/{id} where must {id} be 属于另一个安全组。 Microsoft 365 组不能是安全组成员。
-
https://graph.microsoft.com/v1.0/devices/{id} where {id} 属于设备。
-
https://graph.microsoft.com/v1.0/servicePrincipal/{id} 其中 {id} 属于服务主体。
-
https://graph.microsoft.com/v1.0/orgContact/{id} 其中 {id} 属于组织联系人。
响应
如果成功,此方法返回 204 No Content 响应代码。 当对象已经是组的成员、不支持作为组成员或组是最近创建的并且尚未完全复制错误消息时,它会返回响应 400 Bad Request 代码 (错误消息:“ 源资源对象或正在引用的对象之一不存在。” - 在短暂延迟) 后重试。 当要添加的对象不存在时,它会返回 404 Not Found 响应代码。 它会在以下情况之一中返回 403 Forbidden :
- 你正在尝试将成员添加到 无法通过 Microsoft Graph 管理的组。 此 API 仅支持安全和 Microsoft 365 组。
- 你正在尝试添加无权添加的成员。 有关添加不同成员类型所需的权限,请参阅前面的 权限 部分。
- 你正在尝试将成员添加到可分配角色的组,但你没有所需的权限。
示例
示例 1:将成员添加到组
请求
以下示例演示了使用 directoryObjects 引用将成员添加到组中的请求。
POST https://graph.microsoft.com/beta/groups/{group-id}/members/$ref
Content-type: application/json
{
"@odata.id": "https://graph.microsoft.com/beta/directoryObjects/{id}"
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Models;
var requestBody = new ReferenceCreate
{
OdataId = "https://graph.microsoft.com/beta/directoryObjects/{id}",
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
await graphClient.Groups["{group-id}"].Members.Ref.PostAsync(requestBody);
// Code snippets are only available for the latest major version. Current major version is $v0.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-beta-sdk-go/models"
//other-imports
)
requestBody := graphmodels.NewReferenceCreate()
odataId := "https://graph.microsoft.com/beta/directoryObjects/{id}"
requestBody.SetOdataId(&odataId)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
graphClient.Groups().ByGroupId("group-id").Members().Ref().Post(context.Background(), requestBody, nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.beta.models.ReferenceCreate referenceCreate = new com.microsoft.graph.beta.models.ReferenceCreate();
referenceCreate.setOdataId("https://graph.microsoft.com/beta/directoryObjects/{id}");
graphClient.groups().byGroupId("{group-id}").members().ref().post(referenceCreate);
const options = {
authProvider,
};
const client = Client.init(options);
const directoryObject = {
'@odata.id': 'https://graph.microsoft.com/beta/directoryObjects/{id}'
};
await client.api('/groups/{group-id}/members/$ref')
.version('beta')
.post(directoryObject);
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
use Microsoft\Graph\Beta\Generated\Models\ReferenceCreate;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new ReferenceCreate();
$requestBody->setOdataId('https://graph.microsoft.com/beta/directoryObjects/{id}');
$graphServiceClient->groups()->byGroupId('group-id')->members()->ref()->post($requestBody)->wait();
Import-Module Microsoft.Graph.Beta.Groups
$params = @{
"@odata.id" = "https://graph.microsoft.com/beta/directoryObjects/{id}"
}
New-MgBetaGroupMemberByRef -GroupId $groupId -BodyParameter $params
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.models.reference_create import ReferenceCreate
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = ReferenceCreate(
odata_id = "https://graph.microsoft.com/beta/directoryObjects/{id}",
)
await graph_client.groups.by_group_id('group-id').members.ref.post(request_body)
响应
以下示例显示了相应的响应。
HTTP/1.1 204 No Content
示例 2:在单个请求中向组添加多个成员
此示例说明了如何在 PATCH 操作中通过 OData 绑定支持向组添加多个成员。 单个请求中最多可添加 20 个成员。 如果请求正文中存在错误条件,则不添加任何成员,并且返回相应的响应代码。
请求
以下示例显示了一个请求。
PATCH https://graph.microsoft.com/beta/groups/{group-id}
Content-type: application/json
{
"members@odata.bind": [
"https://graph.microsoft.com/beta/directoryObjects/{id}",
"https://graph.microsoft.com/beta/directoryObjects/{id}",
"https://graph.microsoft.com/beta/directoryObjects/{id}"
]
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Models;
var requestBody = new Group
{
AdditionalData = new Dictionary<string, object>
{
{
"members@odata.bind" , new List<string>
{
"https://graph.microsoft.com/beta/directoryObjects/{id}",
"https://graph.microsoft.com/beta/directoryObjects/{id}",
"https://graph.microsoft.com/beta/directoryObjects/{id}",
}
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Groups["{group-id}"].PatchAsync(requestBody);
// Code snippets are only available for the latest major version. Current major version is $v0.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-beta-sdk-go/models"
//other-imports
)
requestBody := graphmodels.NewGroup()
additionalData := map[string]interface{}{
odataBind := []string {
"https://graph.microsoft.com/beta/directoryObjects/{id}",
"https://graph.microsoft.com/beta/directoryObjects/{id}",
"https://graph.microsoft.com/beta/directoryObjects/{id}",
}
}
requestBody.SetAdditionalData(additionalData)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
groups, err := graphClient.Groups().ByGroupId("group-id").Patch(context.Background(), requestBody, nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
Group group = new Group();
HashMap<String, Object> additionalData = new HashMap<String, Object>();
LinkedList<String> membersOdataBind = new LinkedList<String>();
membersOdataBind.add("https://graph.microsoft.com/beta/directoryObjects/{id}");
membersOdataBind.add("https://graph.microsoft.com/beta/directoryObjects/{id}");
membersOdataBind.add("https://graph.microsoft.com/beta/directoryObjects/{id}");
additionalData.put("members@odata.bind", membersOdataBind);
group.setAdditionalData(additionalData);
Group result = graphClient.groups().byGroupId("{group-id}").patch(group);
const options = {
authProvider,
};
const client = Client.init(options);
const group = {
'members@odata.bind': [
'https://graph.microsoft.com/beta/directoryObjects/{id}',
'https://graph.microsoft.com/beta/directoryObjects/{id}',
'https://graph.microsoft.com/beta/directoryObjects/{id}'
]
};
await client.api('/groups/{group-id}')
.version('beta')
.update(group);
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
use Microsoft\Graph\Beta\Generated\Models\Group;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new Group();
$additionalData = [
'members@odata.bind' => [
'https://graph.microsoft.com/beta/directoryObjects/{id}', 'https://graph.microsoft.com/beta/directoryObjects/{id}', 'https://graph.microsoft.com/beta/directoryObjects/{id}', ],
];
$requestBody->setAdditionalData($additionalData);
$result = $graphServiceClient->groups()->byGroupId('group-id')->patch($requestBody)->wait();
Import-Module Microsoft.Graph.Beta.Groups
$params = @{
"members@odata.bind" = @(
"https://graph.microsoft.com/beta/directoryObjects/{id}"
"https://graph.microsoft.com/beta/directoryObjects/{id}"
"https://graph.microsoft.com/beta/directoryObjects/{id}"
)
}
Update-MgBetaGroup -GroupId $groupId -BodyParameter $params
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.models.group import Group
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = Group(
additional_data = {
"members@odata_bind" : [
"https://graph.microsoft.com/beta/directoryObjects/{id}",
"https://graph.microsoft.com/beta/directoryObjects/{id}",
"https://graph.microsoft.com/beta/directoryObjects/{id}",
],
}
)
result = await graph_client.groups.by_group_id('group-id').patch(request_body)
响应
以下示例显示了相应的响应。
HTTP/1.1 204 No Content
相关内容