Tips for using the Google Calendar API with C#

See my previous post: Interfacing with the Google Calendar API using C#

Tip #1 – Google Calendar API is set to ReadOnly in the example

I needed to edit, not just read.
Change line 23 in the code above:

//static string[] Scopes = { CalendarService.Scope.CalendarReadonly }; // I needed more than read only
static string[] Scopes = { CalendarService.Scope.Calendar };

Tip #2 – Your App’s Security Permission to use the Google Calendar API

My application stopped working. I had to revoke my projects permission and then run the code again so I was prompted to accept permission. Sometimes I had to re-run the code twice to get the prompt.

Go here to revoke your permissions: https://security.google.com/settings/security/permissions

Tip #3 – How to find a Calendar by name

You can get a list of Calendars. The Summary property is where the Calendar title is stored.

    // . . . Authentication here

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


    var list = service.CalendarList.List().Execute();
    var myCalendar = list.Items.SingleOrDefault(c => c.Summary == "My Calendar Name");
            
    // . . . do stuff with your calendar

Tip #4 – How to add an event

I thought it was weird that the example didn’t actually have you add an event. To add an event:

  1. First read Tip #1.
  2. Use this code:
    var list = service.CalendarList.List().Execute();
    var myCalendar = list.Items.SingleOrDefault(c => c.Summary == "My Calendar");
            
    if (myCalendar != null)
    {
        Event calEvent = new Event
        {
            Summary = "Awesome Party",
            Location = "My House",
            Start = new EventDateTime
            {
                DateTime = new DateTime(2015, 5, 20, 19, 00, 0)
            },
            End = new EventDateTime
            {
                DateTime = new DateTime(2015, 5, 20, 23, 59, 0)
            },
            Recurrence = new List<string>()
        };
        var newEventRequest = service.Events.Insert(calEvent, myCalendar.Id);
        var eventResult = newEventRequest.Execute();
    }

One Comment

  1. vikas says:

    I follow steps provided by you but i get exception on
    newEventRequest.Execute();

    Below is the exception
    Google.Apis.Requests.RequestError
    Insufficient Permission [403]
    Errors [
    Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]
    ]

Leave a Reply

How to post code in comments?