Microsoft Graph API-请求不起作用

发布于 2025-02-07 15:46:19 字数 1362 浏览 3 评论 0原文

我试图从Azure广告应用程序中获取用户。通过一些研究,我发现了如何做到这一点。我意识到我需要一个身份验证提供商,所以我遵循了一个链接向我展示了如何选择合适的链接。

我尝试使用客户端秘密提供商,但是当我尝试使用我的GraphServiceClient提出请求时,我的程序会在没有错误的情况下关闭。我尝试了不同的请求,例如:等待GraphClient.users.request()。getAsync()等待GraphClient.me.request().getAsync()。每次我提出请求时,它都会失败。

我还尝试使用我的凭据硬编码使用用户名/密码提供商,但是当我尝试提出请求时,我的程序再次关闭。

我在做什么错?

这就是我现在拥有的:

        var options = new TokenCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
        };

        //var clientSecretCredential = new ClientSecretCredential(TenantId, ClientId, secret, options);

        var userName = "myUserName";
        var password = "myPassword";

        var creds = new UsernamePasswordCredential(userName, password, TenantId, ClientId, options);

        var graphClient = new GraphServiceClient(creds, scopes);

        try
        {
            var users = await graphClient.Users.Request().GetAsync();

            foreach (var user in users)
            {
                Debug.WriteLine(user.DisplayName);
            }
        }
        catch(Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }

I was trying to fetch users from an Azure Ad application. With some research I found how to do that. I realized that I needed an authentication provider, so I followed a this link that showed me how to choose the right one.

I tried with a client secret provider, but when I tried making a request using my GraphServiceClient, my program keeps closing with no errors. I tried different request like : await graphClient.Users.Request().GetAsync() or await graphClient.Me.Request().GetAsync(). Every time I make a request it fails.

I also tried using a Username/password provider with my credentials hard coded, but when I tried making a request my program closed again.

What am I doing wrong?

This is what I have right now:

        var options = new TokenCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
        };

        //var clientSecretCredential = new ClientSecretCredential(TenantId, ClientId, secret, options);

        var userName = "myUserName";
        var password = "myPassword";

        var creds = new UsernamePasswordCredential(userName, password, TenantId, ClientId, options);

        var graphClient = new GraphServiceClient(creds, scopes);

        try
        {
            var users = await graphClient.Users.Request().GetAsync();

            foreach (var user in users)
            {
                Debug.WriteLine(user.DisplayName);
            }
        }
        catch(Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰魂雪魄 2025-02-14 15:46:20

找到了我的问题!

这是关于权限的,我添加了错误的权限。
我添加了委派权限,但是我需要添加应用程序权限
由于我想查看所有用户和组,所以我添加了这些应用程序权限:group.ald.allgroupmember.ald.all and user.read.all.all < /代码>。

多亏了@tiny Wang,我知道我的错误不是在代码中,而是其他地方。

edit

忘了说,但我也更改了等待graphclient.users.request()。getAsync() to graphclient.users.request()。getAsync()。结果使其正常工作。

Found my problem!

It was regarding permissions, I added the wrong permissions.
I added Delegated permissions, but I needed to add Application permissions.
Since I wanted to see all users and groups I added those application permissions: Group.Read.All, GroupMember.Read.All and User.Read.All.

Thanks to @Tiny Wang, I knew that my error wasn't in the code, but elsewhere.

Edit

Forgot to say but I also changed await graphClient.Users.Request().GetAsync() to graphClient.Users.Request().GetAsync().Result to make it work.

你怎么敢 2025-02-14 15:46:20

请创建一个新的ASP.NET Core MVC项目,然后安装azure.Identitymicrosoft.graph nuget软件包。

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "your_tenant_name.onmicrosoft.com";
var clientId = "azure_ad_app_client_id";
var clientSecret = "client_secret";
var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var users = await graphClient.Users.Request().GetAsync();

Please create a new asp.net core MVC project, then install Azure.Identity and Microsoft.Graph nuget package.

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "your_tenant_name.onmicrosoft.com";
var clientId = "azure_ad_app_client_id";
var clientSecret = "client_secret";
var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var users = await graphClient.Users.Request().GetAsync();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文