通过Microsoft Graph,使用C#将事件添加到共享日历

后端开发 2026-07-12

我正在使用MS Graph API,在一个共享日历中添加一个日历事件。我收到这个错误:

IUserEventsCollectionRequestBuilder不包含PostAsync的定义

我找不到其他类似 PostAsync 的方法。

我实际想做的是通过MS Graph在一个共享日历中添加一个事件(在一个ASP.NET / C#的 Web应用程序中)。

请问有人能指点一下吗?

这是我的代码:

private async Task<IUserCalendarsCollectionPage> Add()
{
    var requestBody = new Event
    {
        Subject = "TEST",
        Body = new ItemBody
        {
            ContentType = BodyType.Html,
            Content = "TESTING",
        },
        Start = new DateTimeTimeZone
        {
            DateTime = "2026-03-15T12:00:00",
            TimeZone = "Pacific Standard Time",
        },
        End = new DateTimeTimeZone
        {
            DateTime = "2026-03-15T14:00:00",
            TimeZone = "Pacific Standard Time",
        },
        Location = new Location
        {
            DisplayName = "TESTLoc",
        },
    };

    var scopes = new[] { "User.Read" };

    // Multi-tenant apps can use "common",
    // single-tenant apps must use the tenant ID from the Azure portal
    var tenantId = "some value";

    // Value from app registration
    var clientId = "some value";

    // using Azure.Identity;
    var options = new DeviceCodeCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
        ClientId = clientId,
        TenantId = tenantId,
        DeviceCodeCallback = (code, cancellation) =>
        {
            Console.WriteLine(code.Message);
            return Task.FromResult(0);
        },
    };

    var deviceCodeCredential = new DeviceCodeCredential(options);

    var graphClient = new GraphServiceClient(deviceCodeCredential, scopes);

    var result =  graphClient.Me.Calendars["[email protected]"].Events.PostAsync(requestBody);

    return result;
}

解决方案

你应该使用

graphClient.Me.Calendars["[email protected]"].Events.Request().AddAsync(requestBody);

作为替代方案。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章