[英]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.All
、 GroupMember.Read.All
和User.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.Identity
和Microsoft.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.