无法通过Microsoft图形API从头开始创建团队

时间:2020-10-20 06:48:36

标签: azure-active-directory microsoft-graph-api microsoft-teams

我遵循此文档,并尝试在代码中创建一个团队 https://docs.microsoft.com/en-us/graph/api/team-post?view=graph-rest-1.0&tabs=csharp%2Chttp。 这是我的代码段:

var scopes = new string[] { "https://graph.microsoft.com/.default" };

            // Configure the MSAL client as a confidential client
            var confidentialClient = ConfidentialClientApplicationBuilder
                .Create(clientId)
                .WithTenantId(tenantId)
                .WithClientSecret(clientSecret)
                .Build();
 GraphServiceClient graphServiceClient =
                new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
                {

                    // Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
                    var authResult = await confidentialClient
                        .AcquireTokenForClient(scopes)
                        .ExecuteAsync();

                    // Add the access token in the Authorization header of the API request.
                    requestMessage.Headers.Authorization =
                        new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
                })
                );

            // Make a Microsoft Graph API call
            var team = new Team
            {
                DisplayName = "My Sample Team",
                Description = "My Sample Team’s Description",
                AdditionalData = new Dictionary<string, object>()
    {
        {"template@odata.bind", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"},
        {"members@odata.bind", "[{\"@odata.type\":\"#microsoft.graph.aadUserConversationMember\",\"roles\":[\"owner\"],\"userId\":\"57d4fc1c-f0a3-1111-b41e-22229f05911c\"}]"}
    }
            };
 GraphServiceClient graphServiceClient =
                new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
                {

                    // Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
                    var authResult = await confidentialClient
                        .AcquireTokenForClient(scopes)
                        .ExecuteAsync();

                    // Add the access token in the Authorization header of the API request.
                    requestMessage.Headers.Authorization =
                        new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
                })
                );

            // Make a Microsoft Graph API call
            var team = new Team
            {
                DisplayName = "My Sample Team",
                Description = "My Sample Team’s Description",
                AdditionalData = new Dictionary<string, object>()
    {
        {"template@odata.bind", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"},
        {"members@odata.bind", "[{\"@odata.type\":\"#microsoft.graph.aadUserConversationMember\",\"roles\":[\"owner\"],\"userId\":\"57d4fc1c-f0a3-4105-b41e-1ba89f05911c\"}]"}
    }
            };

但出现此错误:

 "message": "Bind requests not supported for containment navigation property.",\r\n   

我正在使用最新的Microsoft.Graph库,版本是V3.1.8

有人对此问题或odata格式错误有任何想法吗?

1 个答案:

答案 0 :(得分:2)

members@odata.bind似乎仍在变化。当前不起作用。

您需要使用members属性。

POST https://graph.microsoft.com/v1.0/teams

{
  "template@odata.bind":"https://graph.microsoft.com/v1.0/teamsTemplates('standard')",
  "displayName":"My Sample Team555",
  "description":"My Sample Team’s Description555",
  "members":[
      {
         "@odata.type":"#microsoft.graph.aadUserConversationMember",
         "roles":[
            "owner"
         ],
         "userId":"9xxxxxc9-f062-48e2-8ced-22xxxxx6dfce"
      }
   ]
}

相应的C#代码应为:

var team = new Team
            {
                DisplayName = "My Sample Team557",
                Description = "My Sample Team’s Description557",
                Members = (ITeamMembersCollectionPage)new List<ConversationMember>()
                {
                    new AadUserConversationMember
                    {
                        Roles = new List<String>()
                        {
                            "owner"
                        },
                        UserId = "9xxxxxc9-f062-48e2-8ced-22xxxxx6dfce"
                    }
                },
                AdditionalData = new Dictionary<string, object>()
                {
                    {"template@odata.bind", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"}
                }
            };

不幸的是,当我运行代码时,它显示:

System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List`1[Microsoft.Graph.ConversationMember]' to type 'Microsoft.Graph.ITeamMembersCollectionPage'.'

我无法使其工作。解决方法是使用httpClient在您的代码中发送请求。

看到类似的问题here

更新:

我知道了。

您可以尝试以下代码:

        var team = new Team
        {
            DisplayName = "My Sample Team558",
            Description = "My Sample Team’s Description558",
            Members = new TeamMembersCollectionPage() {
                new AadUserConversationMember
                {
                    Roles = new List<String>()
                    {
                        "owner"
                    },
                    UserId = "9xxxxxc9-f062-48e2-8ced-22xxxxx6dfce"
                }
            },
            AdditionalData = new Dictionary<string, object>()
            {
                {"template@odata.bind", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"}
            }
        };

如果您喜欢httpClient方法,请参考此:

        string str = "{\"template@odata.bind\":\"https://graph.microsoft.com/v1.0/teamsTemplates('standard')\",\"displayName\":\"My Sample Team999\",\"description\":\"My Sample Team’s Description555\",\"members\":[{\"@odata.type\":\"#microsoft.graph.aadUserConversationMember\",\"roles\":[\"owner\"],\"userId\":\"9xxxxxc9-f062-48e2-8ced-22xxxxx6dfce\"}]}";

        var content = new StringContent(str, Encoding.UTF8, "application/json");

        HttpClient client = new HttpClient();
        
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        var response = client.PostAsync("https://graph.microsoft.com/v1.0/teams", content).Result;

更新2:

如果您需要用邮递员来称呼它,请使用以下格式:

{
  "template@odata.bind":"https://graph.microsoft.com/v1.0/teamsTemplates('standard')",
  "displayName":"My Sample Team555",
  "description":"My Sample Team’s Description555",
  "members":[
      {
         "@odata.type":"#microsoft.graph.aadUserConversationMember",
         "roles":[
            "owner"
         ],
         "userId":"9xxxxxc9-f062-48e2-8ced-22xxxxx6dfce"
      }
   ]
}