使用 Microsoft Graph API 创建团队时出现错误:“请求中缺少用户的导航绑定”
身体:
我在尝试在 Azure Function 中使用 Microsoft Graph API 创建团队时遇到错误。我收到的错误消息如下:
System.Private.CoreLib: Exception while executing function: Functions.create_azure_ad_group.
System.Private.CoreLib: Result: Failure
Exception: ODataError:
APIError
Code: 400
message: None
error: MainError(additional_data={}, code='BadRequest', details=None, inner_error=InnerError(additional_data={}, client_request_id='0ae9a494-a931-4974-aa17-e96e0f05e11a', date=DateTime(2024, 10, 17, 15, 6, 28, tzinfo=Timezone('UTC')), odata_type=None, request_id='74021609-1bad-43fd-b8e9-034fd1c65fbb'), message='The navigation bind for the user was missing in request', target=None)
这是我用来创建团队的相关代码:
owner_member = AadUserConversationMember(
roles=[
"owner",
],
id=f"https://graph.microsoft.com/v1.0/users('3e3af652-daba-4d47-bfba-968bcc92da04')",
additional_data={
"user@odata_bind": "https://graph.microsoft.com/v1.0/users('3e3af652-daba-4d47-bfba-968bcc92da04')",
}
)
request_body = Team(
display_name=group_name,
description="Communication platform of " + group_name,
additional_data={
"[email protected]": "https://graph.microsoft.com/beta/teamsTemplates('standard')",
},
members=[owner_member]
)
result = await graph_client.teams.post(request_body)
如果您能提供有关如何解决此错误的指导,我将不胜感激。这似乎表明我在请求中绑定用户的方式存在问题。
提前感谢您的帮助!
我的尝试:我已验证在owner_member 对象中使用的用户ID 是否正确并且存在于Azure Active Directory 中。我还确保 user@odata_bind 属性的格式正确以指向同一用户。我尝试使用相同的方法多次创建团队,但错误仍然存在。此外,我查看了用于创建团队和绑定用户的 Microsoft Graph API 文档,但没有找到可能出现问题的明确指示。
我的预期:我希望该请求能够成功创建具有指定所有者成员的团队,并且不会出现任何错误。我预计将使用提供的显示名称、描述以及分配给指定用户的所有者角色来创建团队。
最初,我注册了一个应用程序,并通过授予管理员同意来授予
Team.Create
Application 类型的权限,如下所示:
要使用 Microsoft Graph API 创建团队,您可以使用以下示例 python 代码:
import asyncio
from azure.identity import ClientSecretCredential
from msgraph import GraphServiceClient
from msgraph.generated.models.team import Team
from msgraph.generated.models.aad_user_conversation_member import AadUserConversationMember
CLIENT_ID = 'appId'
CLIENT_SECRET = 'secret'
TENANT_ID = 'tenantId'
SCOPES = ['https://graph.microsoft.com/.default']
credential = ClientSecretCredential(
tenant_id=TENANT_ID,
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET
)
graph_client = GraphServiceClient(credential)
async def create_team(group_name):
owner_member = AadUserConversationMember(
roles=["owner"],
id="https://graph.microsoft.com/v1.0/users('userId')",
additional_data={
"[email protected]": "https://graph.microsoft.com/v1.0/users('userId')",
}
)
request_body = Team(
display_name=group_name,
description=f"Communication platform of {group_name}",
additional_data={
"[email protected]": "https://graph.microsoft.com/beta/teamsTemplates('standard')",
},
members=[owner_member]
)
result = await graph_client.teams.post(request_body)
print("Team created successfully")
asyncio.run(create_team("Demoteam2110"))
回复:
为了确认这一点,我在门户中检查了相同的内容,其中 Microsoft 365 组成功创建了相同的属性,如下所示: