Microsoft Graph API-请求不起作用
我试图从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到了我的问题!
这是关于权限的,我添加了错误的权限。
我添加了
委派权限
,但是我需要添加应用程序权限
。由于我想查看所有用户和组,所以我添加了这些应用程序权限:
group.ald.all
,groupmember.ald.all
anduser.read.all.all < /代码>。
多亏了@tiny Wang,我知道我的错误不是在代码中,而是其他地方。
edit
忘了说,但我也更改了
等待graphclient.users.request()。getAsync()
tographclient.users.request()。getAsync()。结果
使其正常工作。Found my problem!
It was regarding permissions, I added the wrong permissions.
I added
Delegated permissions
, but I needed to addApplication permissions
.Since I wanted to see all users and groups I added those application permissions:
Group.Read.All
,GroupMember.Read.All
andUser.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()
tographClient.Users.Request().GetAsync().Result
to make it work.请创建一个新的ASP.NET Core MVC项目,然后安装
azure.Identity
和microsoft.graph
nuget软件包。Please create a new asp.net core MVC project, then install
Azure.Identity
andMicrosoft.Graph
nuget package.