使用 C# 访问 public Google 日历

Access a public Google Calendar with C#

我有一个 WPF 应用程序,我想在其中访问我已添加到 Google 帐户的 public Google 日历,以便查找即将发生的事件。我使用了 quickstart provided by Google 但我不知道如何 select 我想访问哪个日历。如何选择从哪个日历中获取事件?

更新: 将代码和解决方案移至

使用 CalenderList API 获取日历列表。

根据documentation

Calendar List - A list of all calendars on a user's calendar list in the Calendar UI.

进一步向下滚动,您会发现:

About calendarList entry resources

A calendar in a user's calendar list is a calendar that is visible in the Google Calendar web interface under My calendars or Other calendars:

原来访问日历很简单!我只需要更改

中的 primary
EventsResource.ListRequest request = service.Events.List("primary");

到我要访问的日历的日历 ID。现在我可以从我的主日历中获取我的活动了!

解决方案:

public class GoogleCalendar
{
    static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
    static string ApplicationName = "Calendar API Quickstart";


    public static string GetEvents()
    {
        UserCredential credential = Login();

        return GetData(credential);
    }

    private static string GetData(UserCredential credential)
    {
        // Create Calendar Service.
        var service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

        // Define parameters of request.
        EventsResource.ListRequest request = service.Events.List("The calendar ID to the calender I want to access");
        request.TimeMin = DateTime.Now;
        request.ShowDeleted = false;
        request.SingleEvents = true;
        request.MaxResults = 10;
        request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;

        Console.WriteLine("Upcoming events:");
        Events events = request.Execute();
        if (events.Items.Count > 0)
        {
            foreach (var eventItem in events.Items)
            {
                string when = eventItem.Start.DateTime.ToString();
                if (String.IsNullOrEmpty(when))
                {
                    when = eventItem.Start.Date;
                }
                Console.WriteLine("{0} ({1})", eventItem.Summary, when);
            }
        }
        else
        {
            Console.WriteLine("No upcoming events found.");
        }
        return "See console log for upcoming events";
    }

    static UserCredential Login()
    {
        UserCredential credential;

        using (var stream = new FileStream(@"Components\client_secret.json", FileMode.Open,
            FileAccess.Read))
        {
            string credPath = System.Environment.GetFolderPath(System.Environment
              .SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials");

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
               GoogleClientSecrets.Load(stream).Secrets,
              Scopes,
              "user",
              CancellationToken.None,
              new FileDataStore(credPath, true)).Result;
        }

        return credential;
    }
}