如何将MS Graph API调用转换为GraphServiceClient方法调用?

发布于 2025-02-13 00:31:33 字数 1340 浏览 2 评论 0 原文

查询另一个用户的日历的GraphServiceClient版本是什么?

var cal =等待_graphServiceClient。????? (请参阅下面的代码)

启动代码


builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
         .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"))
           .EnableTokenAcquisitionToCallDownstreamApi()
           .AddInMemoryTokenCaches();

var app = builder.Build();

剃须刀页面代码

using Microsoft.Graph;
using Microsoft.Identity.Web;

namespace MSGraphAPIPOC.Pages;

[Authorize]
[AuthorizeForScopes(Scopes = new[] { "Calendars.ReadWrite" })]
public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger;
    private readonly GraphServiceClient _graphServiceClient;
    public IndexModel(ILogger<IndexModel> logger, GraphServiceClient graphServiceClient)
    {
        _logger = logger;
        _graphServiceClient = graphServiceClient;
    }

    public async Task OnGet()
    {
        ...

        https://graph.microsoft.com/v1.0/users/<calendarSMTP>/calendarview?startDateTime=2022-07-05T00:00:00&endDateTime=2022-07-05T23:59:00&select=start,end,subject

        var cal = await _graphServiceClient.???? //What is the equivalent of the api call above?

        ...
    }
}

任何帮助都将不胜感激。

What is the GraphServiceClient version of querying another user's calendar?

var cal = await _graphServiceClient.???? (see code below)

Startup code


builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
         .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"))
           .EnableTokenAcquisitionToCallDownstreamApi()
           .AddInMemoryTokenCaches();

var app = builder.Build();

Razor pages code

using Microsoft.Graph;
using Microsoft.Identity.Web;

namespace MSGraphAPIPOC.Pages;

[Authorize]
[AuthorizeForScopes(Scopes = new[] { "Calendars.ReadWrite" })]
public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger;
    private readonly GraphServiceClient _graphServiceClient;
    public IndexModel(ILogger<IndexModel> logger, GraphServiceClient graphServiceClient)
    {
        _logger = logger;
        _graphServiceClient = graphServiceClient;
    }

    public async Task OnGet()
    {
        ...

        https://graph.microsoft.com/v1.0/users/<calendarSMTP>/calendarview?startDateTime=2022-07-05T00:00:00&endDateTime=2022-07-05T23:59:00&select=start,end,subject

        var cal = await _graphServiceClient.???? //What is the equivalent of the api call above?

        ...
    }
}

Any help would be appreciated.

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

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

发布评论

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

评论(1

神经暖 2025-02-20 00:31:33

有两种类型的MS Graph API权限,一个是委托,这意味着用户可以先登录,然后通过MS Graph API查询自己的信息。另一种类型是 Application ,这意味着应用程序可以通过Graph API查询所有用户的信息。

回到您的方案,您将Azure AD集成到您的ASP.NET Core Web应用程序中,这意味着用户必须先登录,然后访问 index 页面,对吗?因此,您现在使用委托 API许可,允许您使用等待_graphserviceclient.me.calendarview.request.request.request(queryOptions).getAsync().getAsync()日历视图,但没有许可来查询他人。

如果要查询其他人,则必须同意 application API许可。在您的情况下, API 支持应用程序许可。然后在本节添加权限。然后使用下面的代码或此示例部分致电API:

using Azure.Identity;
using Microsoft.Graph;

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "your_tenant_name.onmicrosoft.com";
var clientId = "azure_ad_app_id";
var clientSecret = "client_secret";
var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var queryOptions = new List<QueryOption>()
{
    new QueryOption("startDateTime", "2022-07-05T00:00:00"),
    new QueryOption("endDateTime", "2022-07-05T23:59:00")
};
var res = await graphClient.Users["user_id"].CalendarView.Request(queryOptions).Select("start,end,subject").GetAsync();

There are 2 types of ms graph api permission, one is delegate which means users can sign in first and then query their own information via ms graph api. Another type is Application, this means application can query all users' information via graph api.

Come back to your scenario, you integrate azure ad into your asp.net core web application, which means users have to sign in first before then visit Index page right? So you are now using the delegate api permission, which allowing you to use await _graphServiceClient.Me.CalendarView.Request( queryOptions ).GetAsync() to query his/her own calendar view but don't have permission to query others.

If you want to query others, you have to consent application api permission. In your scenario, the api supports application permission. Then following the screenshot in this section to add the permission. Then using code below or this sample section to call the api:

using Azure.Identity;
using Microsoft.Graph;

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "your_tenant_name.onmicrosoft.com";
var clientId = "azure_ad_app_id";
var clientSecret = "client_secret";
var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var queryOptions = new List<QueryOption>()
{
    new QueryOption("startDateTime", "2022-07-05T00:00:00"),
    new QueryOption("endDateTime", "2022-07-05T23:59:00")
};
var res = await graphClient.Users["user_id"].CalendarView.Request(queryOptions).Select("start,end,subject").GetAsync();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文