如何使用C#使用Office 365 API为组或特定电子邮件帐户创建别名?
我想为已在Office 365中创建的组(或特定电子邮件)动态创建组别名。
答案 0 :(得分:1)
在这种情况下,您可以考虑使用Microsoft Graph - Create Group
首先,确保您已分配“Microsoft Graph”> “读取并写入所有组”对Azure AD中的应用程序的权限。
代码供您参考:
string authority = "https://login.windows.net/yourdomain.onmicrosoft.com";
string clientId = "{client_id}";
Uri redirectUri = new Uri("http://localhost");
string resourceUrl = "https://graph.microsoft.com";
HttpClient client = new HttpClient();
AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);
AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resourceUrl,
clientId, redirectUri, PromptBehavior.Always);
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + authenticationResult.AccessToken);
string content = @"{
'displayName': 'mailgrouptest',
'groupTypes': ['Unified'],
'mailEnabled': true,
'mailNickname': 'mailalias1',
'securityEnabled': false
}";
var httpContent = new StringContent(content, Encoding.GetEncoding("utf-8"), "application/json");
var response = client.PostAsync("https://graph.microsoft.com/v1.0/groups", httpContent).Result;
Console.WriteLine(response.Content.ReadAsStringAsync().Result);