使用 Graph API 和委派权限将成员添加到 Microsoft Teams
Add a member to Microsoft Teams using Graph API and delegated permissions
我正在尝试简单地将成员(已经在组织中)添加到特定的 Microsoft 团队。 observerID 是我要添加的成员的 ID,teamID 是特定团队的 ID。我在启用 TeamMembers.ReadWrite.All 的情况下使用委派权限。
我的代码如下所示:
string json = $@"
{{
""@odata.type"": ""#microsoft.graph.aadUserConversationMember"",
""roles"": [""member""],
""user@odata.bind"": ""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;
}
我的问题是 http 调用失败,状态码为 400; 'Bad Request'。我一次又一次地尝试找出我的电话有任何问题,但我似乎找不到问题所在。当我 Console.WriteLine 我使用 json 作为正文时,它看起来像这样:
{
"odata.type": "#microsoft.graph.aadUserConversationMember",
"roles": ["member"],
"user@odata.bind": "https://graph.microsoft.com/beta/users(d52c2663-1c41-401b-8015-1216f0e68960)"
}
url 看起来像:“https://graph.microsoft.com/beta/teams/a9f9ac33-fba5-4ce2-9515-8c498c70af85/members”,当我尝试通过 Postman 调用时,它仍然是 returns 错误代码400.
有没有人知道哪里出了问题?
其实这个错误很简单。报400一般是参数错误。您的 json 文件缺少特殊符号 @
和 "
。我在本地测试了它并为我工作。
我正在尝试简单地将成员(已经在组织中)添加到特定的 Microsoft 团队。 observerID 是我要添加的成员的 ID,teamID 是特定团队的 ID。我在启用 TeamMembers.ReadWrite.All 的情况下使用委派权限。 我的代码如下所示:
string json = $@"
{{
""@odata.type"": ""#microsoft.graph.aadUserConversationMember"",
""roles"": [""member""],
""user@odata.bind"": ""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;
}
我的问题是 http 调用失败,状态码为 400; 'Bad Request'。我一次又一次地尝试找出我的电话有任何问题,但我似乎找不到问题所在。当我 Console.WriteLine 我使用 json 作为正文时,它看起来像这样:
{
"odata.type": "#microsoft.graph.aadUserConversationMember",
"roles": ["member"],
"user@odata.bind": "https://graph.microsoft.com/beta/users(d52c2663-1c41-401b-8015-1216f0e68960)"
}
url 看起来像:“https://graph.microsoft.com/beta/teams/a9f9ac33-fba5-4ce2-9515-8c498c70af85/members”,当我尝试通过 Postman 调用时,它仍然是 returns 错误代码400.
有没有人知道哪里出了问题?
其实这个错误很简单。报400一般是参数错误。您的 json 文件缺少特殊符号 @
和 "
。我在本地测试了它并为我工作。