使用 Graph API 创建 AD 组时出现问题
Issue while creating a AD Group using Graph API
我正在尝试使用 Graph API 和以下代码编写简单的 .net 控制台来创建 Azure AD 组,但是代码没有返回任何错误消息,而且当我尝试 运行 组没有被创建的代码。
我做错了什么。
using Microsoft.Identity.Client;
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using System;
using System.Collections.Generic;
namespace AADConsole2
{
class Program
{
private const string tenantId = "<<tenantid>>";
private const string clientId = "<<client id>>";
private static string appKey = "<<client secret>>";
static void Main(string[] args)
{
CreateADGroup();
}
public static async void CreateADGroup()
{
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantId)
.WithClientSecret(appKey)
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var testGroup = new Group
{
Description = "testgroupdescription",
DisplayName = "testgroupkk",
GroupTypes = new List<String>()
{},
MailEnabled = false,
MailNickname = "testnickname",
SecurityEnabled = true,
//AdditionalData = additionalDataGroupB
};
await graphClient.Groups.Request().AddAsync(testGroup);
}
}
}
我测试了你的代码,虽然没有报错,但是确实无法建群。因为你的代码好像不能获取到token,而且你代码中没有设置scope
我使用控制台应用程序编写了测试代码,本地测试可以完美创建组。你可以试试:
using System;
using Microsoft.Identity.Client;
using Microsoft.Graph.Auth;
using Microsoft.Graph;
using System.Collections.Generic;
namespace test
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
IConfidentialClientApplication app;
app = ConfidentialClientApplicationBuilder.Create("{client id}")
.WithClientSecret("{Client Secret}")
.WithAuthority(new Uri("https://login.microsoftonline.com/{tenant}"))
.Build();
AuthenticationResult result = null;
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
string accesstoken = result.AccessToken;
/*Console.WriteLine(accesstoken);*/
ClientCredentialProvider authProvider = new ClientCredentialProvider(app);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var testGroup = new Group
{
Description = "testgroupdescription",
DisplayName = "testgroup1",
GroupTypes = new List<String>()
{ },
MailEnabled = false,
MailNickname = "testnickname",
SecurityEnabled = true,
//AdditionalData = additionalDataGroupB
};
await graphClient.Groups.Request().AddAsync(testGroup);
}
}
}
我正在尝试使用 Graph API 和以下代码编写简单的 .net 控制台来创建 Azure AD 组,但是代码没有返回任何错误消息,而且当我尝试 运行 组没有被创建的代码。
我做错了什么。
using Microsoft.Identity.Client;
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using System;
using System.Collections.Generic;
namespace AADConsole2
{
class Program
{
private const string tenantId = "<<tenantid>>";
private const string clientId = "<<client id>>";
private static string appKey = "<<client secret>>";
static void Main(string[] args)
{
CreateADGroup();
}
public static async void CreateADGroup()
{
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantId)
.WithClientSecret(appKey)
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var testGroup = new Group
{
Description = "testgroupdescription",
DisplayName = "testgroupkk",
GroupTypes = new List<String>()
{},
MailEnabled = false,
MailNickname = "testnickname",
SecurityEnabled = true,
//AdditionalData = additionalDataGroupB
};
await graphClient.Groups.Request().AddAsync(testGroup);
}
}
}
我测试了你的代码,虽然没有报错,但是确实无法建群。因为你的代码好像不能获取到token,而且你代码中没有设置scope
我使用控制台应用程序编写了测试代码,本地测试可以完美创建组。你可以试试:
using System;
using Microsoft.Identity.Client;
using Microsoft.Graph.Auth;
using Microsoft.Graph;
using System.Collections.Generic;
namespace test
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
IConfidentialClientApplication app;
app = ConfidentialClientApplicationBuilder.Create("{client id}")
.WithClientSecret("{Client Secret}")
.WithAuthority(new Uri("https://login.microsoftonline.com/{tenant}"))
.Build();
AuthenticationResult result = null;
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
string accesstoken = result.AccessToken;
/*Console.WriteLine(accesstoken);*/
ClientCredentialProvider authProvider = new ClientCredentialProvider(app);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var testGroup = new Group
{
Description = "testgroupdescription",
DisplayName = "testgroup1",
GroupTypes = new List<String>()
{ },
MailEnabled = false,
MailNickname = "testnickname",
SecurityEnabled = true,
//AdditionalData = additionalDataGroupB
};
await graphClient.Groups.Request().AddAsync(testGroup);
}
}
}