Search code examples
c#microsoft-graph-apimicrosoft-graph-teams

Add a member to Microsoft Teams using Graph API and delegated permissions


I am trying to simply add a member (who is already in the organization) to a specific Microsoft Team. The observerID is the id of the the member that I want to add and teamID is is the ID of the specific Team. I am using delegated permission with TeamMembers.ReadWrite.All enabled. My code looks like this:

            string json = $@"
            {{
              ""@odata.type"": ""#microsoft.graph.aadUserConversationMember"",
              ""roles"": [""member""],
              ""[email protected]"": ""https://graph.microsoft.com/beta/users({observerID})""
            }}";
            
            var body = new StringContent(json, Encoding.UTF8, "application/json");
            Console.WriteLine("Add observer");
            return await protectedApiCallHelper.CallWebApiAsync(WebApiUrlTeams + teamID + "/members", accessToken, body);
public async Task<JObject> CallWebApiAsync(string webApiUrl, string accessToken, HttpContent content)
        {
            if (!string.IsNullOrEmpty(accessToken))
            {
                var defaultRequestHeaders = HttpClient.DefaultRequestHeaders;
                if (defaultRequestHeaders.Accept == null || !defaultRequestHeaders.Accept.Any(m => m.MediaType == "application/json"))
                {
                    HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                }
                defaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", accessToken);

                    HttpResponseMessage response = await HttpClient.PostAsync(webApiUrl, content);
                if (response.IsSuccessStatusCode)
                {
                    string json = await response.Content.ReadAsStringAsync();
                    JObject result = JsonConvert.DeserializeObject(json) as JObject;
                    return result;
                }
            }
            return null;
        }

My problem is that the http call fails with the status code 400; 'Bad Request'. I have tried again and again to find any issues with my call but I can't seem to find the problem. When I Console.WriteLine the json I use for the body it looks like this:

            {
              "odata.type": "#microsoft.graph.aadUserConversationMember",
              "roles": ["member"],
              "[email protected]": "https://graph.microsoft.com/beta/users(d52c2663-1c41-401b-8015-1216f0e68960)"
            }

And the url looks like: "https://graph.microsoft.com/beta/teams/a9f9ac33-fba5-4ce2-9515-8c498c70af85/members" and when I try the call through Postman it still returns a error code 400.

Does anyone have any insight on what might be wrong?


Solution

  • In fact, this error is very simple. Reporting 400 is usually a parameter error. Your json file is missing the special symbols @ and ". I tested it locally and worked for me.

    enter image description here