Microsoft graph api 使用Graph API创建广告组时出现问题

Microsoft graph api 使用Graph API创建广告组时出现问题,microsoft-graph-api,azure-ad-graph-api,Microsoft Graph Api,Azure Ad Graph Api,我正在尝试编写简单的.net控制台,使用Graph API和以下代码创建Azure AD组,但是代码没有返回任何错误消息,而且当我尝试运行代码时,组也没有被创建 我做错了什么事 using Microsoft.Identity.Client; using Microsoft.Graph; using Microsoft.Graph.Auth; using System; using System.Collections.Generic; namespace AADConsole2 {

我正在尝试编写简单的.net控制台,使用Graph API和以下代码创建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);


        }
    }
}

使用Microsoft.Identity.Client;
使用Microsoft.Graph;
使用Microsoft.Graph.Auth;
使用制度;
使用System.Collections.Generic;
命名空间AADConsole2
{
班级计划
{
private const string tenantId=“”;
private const string clientId=“”;
私有静态字符串appKey=“”;
静态void Main(字符串[]参数)
{
CreateADGroup();
}
公共静态异步void CreateADGroup()
{
IConfidentialClientApplication-secretentialclientapplication=secretentialclientapplicationbuilder
.Create(clientId)
.WithTenantId(tenantId)
.WithClientSecret(appKey)
.Build();
ClientCredentialProvider authProvider=新的ClientCredentialProvider(机密客户端应用程序);
GraphServiceClient graphClient=新的GraphServiceClient(authProvider);
var testGroup=新组
{
Description=“testgroupdescription”,
DisplayName=“testgroupkk”,
GroupTypes=新列表()
{},
MailEnabled=false,
MailNickname=“test昵称”,
SecurityEnabled=true,
//AdditionalData=additionalDataGroupB
};
等待graphClient.Groups.Request().AddAsync(testGroup);
}
}
}

我测试了您的代码,虽然没有报告错误,但确实不可能创建一个组。因为您的代码似乎无法获取令牌,并且您没有在代码中设置
范围

我使用控制台应用程序编写了测试代码,本地测试可以完美地创建组。你可以试试:

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);


        }
    }
}
    
使用系统;
使用Microsoft.Identity.Client;
使用Microsoft.Graph.Auth;
使用Microsoft.Graph;
使用System.Collections.Generic;
名称空间测试
{
班级计划
{
静态异步System.Threading.Tasks.Task Main(字符串[]args)
{
IConfidentialClient应用程序;
app=secretentialClientApplicationBuilder.Create(“{client id}”)
.WithClientSecret(“{Client Secret}”)
.WithAuthority(新Uri(“https://login.microsoftonline.com/{租户})
.Build();
AuthenticationResult=null;
字符串[]范围=新字符串[]{”https://graph.microsoft.com/.default" };
结果=等待app.AcquireTokenForClient(scopes.ExecuteAsync();
字符串accesstoken=result.accesstoken;
/*Console.WriteLine(accesstoken)*/
ClientCredentialProvider authProvider=新的ClientCredentialProvider(应用程序);
GraphServiceClient graphClient=新的GraphServiceClient(authProvider);
var testGroup=新组
{
Description=“testgroupdescription”,
DisplayName=“testgroup1”,
GroupTypes=新列表()
{ },
MailEnabled=false,
MailNickname=“test昵称”,
SecurityEnabled=true,
//AdditionalData=additionalDataGroupB
};
等待graphClient.Groups.Request().AddAsync(testGroup);
}
}
}

您授予了哪些权限?我已授予以下API权限目录。readwrite,directory.read,group.create,group.readwriter是否授予应用程序权限或委派权限?这是一个应用程序权限hanks alot,它很有效。@krishna如果我的答案对您有帮助,您可以接受它作为答案(点击答案旁边的复选标记,将其从灰色变为已填写。)请看这对其他社区成员是有益的。谢谢。@krishna Hi,如果您有任何问题,请随时打电话给我。