Accessing public google calendar using c #

I have a WPF application where you want to access a public Google Calendar that I have added to my Google account to search for upcoming events. I have used the quickstart provided by Google , but I cannot figure out how to choose which calendar I want to access. How do I choose a calendar to receive events?

UPDATE: Moved code and solution for a separate answer .

+3


source to share


2 answers


It turns out it was easy to access the calendar! I just needed to change primary

into

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

      

for the calendar id for the calendar I want to access. Now I can get my events from my main calendar!



Decision:

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;
    }
}

      

0


source


Get the calendar list with CalenderList API

.

According to the documentation :

Calendar List - A list of all calendars in the users calendar list in the calendar UI.



scroll further down:

About calendarList entry resources

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

+1


source







All Articles