简体   繁体   English

Microsoft Graph Api - 请求不起作用

[英]Microsoft Graph Api - request not working

I was trying to fetch users from an Azure Ad application.我试图从 Azure 广告应用程序中获取用户。 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.我尝试使用客户端密钥提供程序,但是当我尝试使用我的GraphServiceClient发出请求时,我的程序不断关闭而没有错误。 I tried different request like : await graphClient.Users.Request().GetAsync() or await graphClient.Me.Request().GetAsync() .我尝试了不同的请求,例如: await graphClient.Users.Request().GetAsync()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);
        }

Found my problem!发现我的问题!

It was regarding permissions, I added the wrong permissions.这是关于权限的,我添加了错误的权限。 I added Delegated permissions , but I needed to add Application permissions .我添加了Delegated permissions ,但我需要添加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 .因为我想查看所有用户和组,所以我添加了这些应用程序权限: Group.Read.AllGroupMember.Read.AllUser.Read.All

Thanks to @Tiny Wang, I knew that my error wasn't in the code, but elsewhere.感谢@Tiny Wang,我知道我的错误不在代码中,而是在其他地方。

Edit编辑

Forgot to say but I also changed await graphClient.Users.Request().GetAsync() to graphClient.Users.Request().GetAsync().Result to make it work.忘了说,但我也将await graphClient.Users.Request().GetAsync()更改为graphClient.Users.Request().GetAsync().Result以使其工作。

Please create a new asp.net core MVC project, then install Azure.Identity and Microsoft.Graph nuget package.请创建一个新的 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft Graph API,申请权限时需要Region - Microsoft Graph API, Region is required when request with application permission Microsoft graph api 更新事件返回 HTTP 错误 400 错误请求 - Microsoft graph api update event returns HTTP error 400 bad request Microsoft Graph API 身份验证错误(错误描述:/me request is only valid with delegated authentication flow) - Microsoft Graph API authentication error(Error Description: /me request is only valid with delegated authentication flow) Microsoft Graph API-以其他用户身份发送电子邮件 - Microsoft Graph API - Sending email as another user ASP .NET Core MVC和Microsoft Graph API - ASP .NET core mvc & Microsoft Graph API 从Microsoft Graph Api接收图像 - Receiving image from Microsoft Graph Api 如何使用 Microsoft Graph API 更新个人资料图片 - How to Update Profile Picture with Microsoft Graph API 通过Microsoft Graph API创建Excel文件 - Create excel file through Microsoft Graph API WEB.API Core,服务器查询到Microsoft Graph或Outlook - WEB.API Core , Server Query To Microsoft Graph or Outlook 如何使用 Microsoft graph api 将 colors 应用于 outlook 日历事件? - How to apply colors to outlook calendar events using Microsoft graph api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM