只是为了添加到 Allen Wu 的答案。例如,这就是您在 c# 中使用它的方式。
string json = @"
{
""requests"": [{
""id"": ""1"",
""method"": ""POST"",
""url"": ""/users"",
""body"": {
""accountEnabled"": true,
""displayName"": ""zetawarsTest01"",
""mailNickname"": ""zetawarstest01"",
""userPrincipalName"": ""zetawarstest01@projecttarget.onmicrosoft.com"",
""passwordProfile"": {
""forceChangePasswordNextSignIn"": false,
""password"": ""zetawars123!@#""
}
},
""headers"": {
""Content-Type"": ""application/json""
}
}, {
""id"": ""2"",
""method"": ""POST"",
""url"": ""/users"",
""body"": {
""accountEnabled"": true,
""displayName"": ""zetawarsTest02"",
""mailNickname"": ""zetawarsTest02"",
""userPrincipalName"": ""zetawarsTest02@projecttarget.onmicrosoft.com"",
""passwordProfile"": {
""forceChangePasswordNextSignIn"": false,
""password"": ""zetawars123!@#""
}
},
""headers"": {
""Content-Type"": ""application/json""
}
}
]
}
";
//Credentials
var tenantId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var clientId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var clientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var scopes = new string[] { "https://graph.microsoft.com/.default" };
//This to get the Access Token
var confidentialClient = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}/v2.0")
.WithClientSecret(clientSecret)
.Build();
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantId)
.WithClientSecret(clientSecret)
.Build();
var result = await confidentialClientApplication
.AcquireTokenForClient(new string[] { "https://graph.microsoft.com/.default" })
.ExecuteAsync(); ;
// This is to send Json Request using Client
using (HttpClient http = new HttpClient())
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://graph.microsoft.com/v1.0/$batch"))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
if (!string.IsNullOrEmpty(json))
{
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
using (HttpResponseMessage response = await http.SendAsync(request))
{
string error = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
if (response.StatusCode == (HttpStatusCode)429)
{
// TBD: Add you error handling here
}
throw new Exception(error);
}
await response.Content.ReadAsStringAsync();
}
}
我已阅读批量限制为 20 个用户/请求。但我还没有测试过。