使用 Gmail 客户端管理 Google 日历

添加、编辑和删除 Gmail 日历

Aspose.Email 允许应用程序使用以下方式管理 Gmail 日历 IGmailClient 提供添加、删除和更新 Gmail 日历等功能。此客户端类返回一个 ExtendedCalendar 类型对象列表,包含 Gmail 日历项目的信息。 IGmailClient 类公开以下日历功能:

要访问日历,使用 Gmail 帐户凭据初始化 GoogleTestUser。GoogleOAuthHelper 用于获取用户的访问令牌,随后用于初始化 IGmailClient。

插入、获取和更新 Gmail 日历

要插入日历,请初始化 Calendar 类型对象并使用以下方法插入 CreateCalendar() 函数。 CreateCalendar() 返回新插入日历的 ID。该 ID 可用于从服务器检索日历。以下代码片段展示了如何插入、检索和更新日历。

删除特定的 Google 日历

要删除特定日历,需要先获取客户端所有日历的列表,然后按需删除。 ListCalendars() 返回列表 ExtendedCalendar 其中包含 Gmail 日历。以下代码片段展示了如何删除特定日历。

日历访问控制

Aspose.Email 提供对日历项目访问的完整控制。 ListAccessRules() 函数由以下提供 IGmailClient 返回列表 AccessControlRule。可以检索、修改并重新保存客户端日历的单个规则信息。 IGmailClient 包含以下用于管理访问控制规则的函数。

以下代码片段展示了如何使用函数来管理访问规则:

日历客户端设置和颜色信息

Aspose.Email 支持通过使用来访问客户端设置 IGmailClient.GetSettings()。它返回如下设置列表:

  1. dateFieldOrder
  2. displayAllTimezones
  3. hideInvitations
  4. format24HourTime
  5. defaultCalendarMode
  6. defaultEventLength
  7. locale
  8. remindOnRespondedEventsOnly
  9. alternateCalendar
  10. userLocation
  11. hideWeekends
  12. showDeclinedEvents
  13. weekStart
  14. weather
  15. customCalendarMode
  16. timezoneLabel
  17. timezone
  18. useKeyboardShortcuts
  19. country

同样,也可以使用以下方式检索客户端的颜色信息 IGmailClient.GetColors()。此颜色信息对象返回前景色、背景色以及更新日期和时间的列表。

访问客户端设置

以下代码片段展示了如何使用这些函数访问客户端设置:

访问颜色信息

以下代码片段展示了如何使用这些函数访问客户端颜色设置。

管理 Google 日历预约

Aspose.Email 提供在 Google 日历中处理预约的功能。以下任务可在 Google 日历的预约上执行:

  1. 添加预约。
  2. 检索预约列表。
  3. 检索特定预约。
  4. 更新约会。
  5. 将预约从一个日历移动到另一个日历。
  6. 删除预约。

IGmailClient 提供诸如下面的函数: CreateAppointment, FetchAppointment, UpdateAppointment, ListAppointments, MoveAppointmentDeleteAppointment.

向 Google 日历添加约会

以下代码示例演示在日历中添加约会的功能。要实现此功能,请按照以下步骤操作:

  1. 创建并插入日历。
  2. 从新日历检索约会列表。
  3. 创建约会。
  4. 插入一个约会。

检索并更新 Google 日历预约

下面演示了日历的检索和更新过程:

  1. 检索特定约会。
  2. 修改约会。
  3. 在日历中更新约会。

假设已获取日历 ID 为 "calendarId" 且预约唯一 ID 为 "AppointmentUniqueId" 的日历。以下代码片段展示了如何检索并更新预约。

在 Google 日历中移动和删除预约

通过提供源日历、目标日历以及源日历中预约的唯一 ID,可以移动预约。以下代码片段展示了如何移动和删除预约。

Google 日历的 FreeBusy 查询

Aspose.Email 提供查询机制,根据条件检查预约是否到期。为此提供了 FreebusyQuery 类,可用于为特定日历准备查询。

此代码示例演示了查询日历的功能。该示例执行了以下任务:

  1. 创建并插入日历
  2. 创建预约
  3. 插入预约
  4. 准备 FreeBusyQuery
  5. 获取 FreebusyResponse
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET

// Use the GoogleUser and GoogleOAuthHelper classes below to receive an access token
using (IGmailClient client = GmailClient.GetInstance(accessToken, user.Email))
{
    // Initialize calendar item
    Aspose.Email.Clients.Google.Calendar calendar1 = new Aspose.Email.Clients.Google.Calendar("summary - " + Guid.NewGuid().ToString(), null, null, "Europe/Kiev");

    // Insert calendar and get back id of newly inserted calendar and Fetch the same calendar using calendar id
    string id = client.CreateCalendar(calendar1);
    Aspose.Email.Clients.Google.Calendar cal1 = client.FetchCalendar(id);
    string calendarId1 = cal1.Id;
    try
    {
        // Get list of appointments in newly inserted calendar. It should be zero
        Appointment[] appointments = client.ListAppointments(calendarId1);
        if (appointments.Length != 0)
        {
            Console.WriteLine("Wrong number of appointments");
            return;
        }

        // Create a new appointment and Calculate appointment start and finish time
        DateTime startDate = DateTime.Now;
        DateTime endDate = startDate.AddHours(1);

        // Create attendees list for appointment
        MailAddressCollection attendees = new MailAddressCollection();
        attendees.Add("user1@domain.com");
        attendees.Add("user2@domain.com");

        // Create appointment
        Appointment app1 = new Appointment("Location - " + Guid.NewGuid().ToString(), startDate, endDate, "user2@domain.com", attendees);
        app1.Summary = "Summary - " + Guid.NewGuid().ToString();
        app1.Description = "Description - " + Guid.NewGuid().ToString();
        app1.StartTimeZone = "Europe/Kiev";
        app1.EndTimeZone = "Europe/Kiev";

        // Insert the newly created appointment and get back the same in case of successful insertion
        Appointment app2 = client.CreateAppointment(calendarId1, app1);

        // Create Freebusy query by setting min/max timeand time zone
        FreebusyQuery query = new FreebusyQuery();
        query.TimeMin = DateTime.Now.AddDays(-1);
        query.TimeMax = DateTime.Now.AddDays(1);
        query.TimeZone = "Europe/Kiev";

        // Set calendar item to search and Get the reponse of query containing 
        query.Items.Add(cal1.Id);
        FreebusyResponse resp = client.GetFreebusyInfo(query);
        // Delete the appointment
        client.DeleteAppointment(calendarId1, app2.UniqueId);
    }
    finally
    {
        // Delete the calendar
        client.DeleteCalendar(cal1.Id);
    }
}