Public Google Calendars with C# 16


[wp_ad_camp_5]

Google calendar large

Are you trying to access a public Google calendar?   Would you like to see a list of holidays for your country, phases of the moon, week numbers, or sports events, did you know that there are public Google Calendars available with this information?    In this post we are going to look how to access public Google Calendars using a public API key created on Google Developers console.    For this post I will be using Visual Studio C# .net and the Google .net client library, I will post a similar example using the PHP client library soon.

Prerequisite

We are going to be using a public API key in this post.  If you don’t know how to create a public API key I recommend you check my Beginning Google development post series specifically the public API key post which explains about public data vs private data and how to create a public API key on the Google Developers console.

Make sure you have enabled the Google Calendar API in your project in Google Developers console.   If you don’t know how to do that you can again check my Beginning Google development post series specifically the post about Enabling APIs which speaks about the different APIs and quotas.

Setting a calendar public

You can set one of your own calendars to public through the Google Calendar website.   When you make a calendar public, other people can:

  • Find your public calendar by searching on Google.
  • See your public calendar on a website or synced with another application.
  • Subscribe to your public calendar and view it within Google Calendar.
  • View your public calendar without using Google Calendar.
  • When you make a calendar public, you can choose to let other people see all the event details or just when you are free or busy.

How to set a Google calendar public

  1. On a computer, open Google Calendar.
  2. In the top right, click Settings settings gear button > Settings.
  3. Open the Calendars tab.
  4. Click the name of the calendar you want to share.
  5. Open the Share this Calendar tab.
  6. Check the option Make this calendar public.
  7. If don’t want other people to view the details of your events, select See only free/busy (hide details).
  8. Click Save.

Public Google Calendars

Google has a collection of Interesting Calendars they mostly contain events with regard to holidays, phases of the moon, week numbers, and sports events.   

  1. On a computer, open Google Calendar
  2. On the bottom left hand side find other calendars, click the arrow down and find browse Interesting calendars in the menu
  3. On the top you will find three tabs holidays , sports and more
  4. To add a calendar to your calendar list you can click subscribe.
  5. Go back to Google calendar the new calendar should be in the list now.

Programmatic access a public Google calendar

In order to access a public calendar you need to know its calendar ID, there is no way to search all public calendars.   The only way to find a public calendar id of one of Googles interesting calendars that I am aware of is to add it to your Google calendar.      If this is your own calendar that you have set public you could also use calendarlist.list method to get a list of all your calendars and find the ID that way.   However in my opinion its probably easier still to just check the Google Calendar website.

Find Calendar ID

  1. Click the arrow next to the new calendar, go to Settings
  2. find Calendar Address:  and the id for you calendar en.danish#holiday@group.v.calendar.google.com

List events HTTP Get

The call to list events is like most of the other calls to the Google APIs it is a simple HTTP Get, because of that we can place the following in a web browser and it will return the data to us.

GET https://www.googleapis.com/calendar/v3/calendars/calendarId/events?key=[APIKey]

You can also use Googles .net client library to see the data much easier.

C# example

From Microsoft visual studio we import the NuGet Package for the Google .Net client library

PM> Install-Package Google.Apis.Calendar.v3

Make sure you project is set to either .Net framework 4.0 or 4.5

Calendar service

Because we are using a public calendar we don’t need to authenticate the client library.    All we need to do is create a CalendarService and pass it the API key we created in Google Developers console.

 var service = new CalendarService(new BaseClientService.Initializer()
        {
         ApiKey = "XXX",
         ApplicationName = "xyz",

        });

[wp_ad_camp_3]

Requesting data

Once we have created our CalendarService we can use it to retrieve events for the public calendar

var events = service.Events.List("en.danish#holiday@group.v.calendar.google.com").Execute();

foreach (var myEvent in events.Items)
     {
     Console.WriteLine(string.Format("Event: {0} Start: {1} End: {2}", myEvent.Summary, myEvent.Start.DateTime.ToString(), myEvent.End.DateTime.ToString()));
     }

Conclusion

Once we have created a public API key on Google Developers console it gives us access to a wealth of public data on Googles servers. One of the things that can be very useful is accessing public calendars that Google has created for us. With the standard public calendars you can find holiday information as well as other things.


About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

16 thoughts on “Public Google Calendars with C#

  • Preguntón Cojonero Cabrón

    Great, very useful!

    Now, for my private calendar for my preguntoncojonero @ gmail . com account, is it possible get access programmatically to private Google Calendar?

    I have too a service account in Google Developers Console.

    Thanks.

  • Parag Ghadge

    how to create new calendar in google calendar with c#.
    I am able to add new entry in google calendar, but it is not going into the calendar box. It showing on top of calendar.
    I have accessed google calendar through google calendar API.

  • Supreet

    This is a great tutorial. It really helped me out to get my google calendar working using the api key. Is there a way I can sort my results that are pulled from the calendar by the start date and time of the event on the calendar?

        • Supreet

          I am able to pull all the events from my google calendar for the current day. I was wondering if there is a way to display events for only the next day? I used the following code to get a list of the current day’s events.

          EventsResource.ListRequest request = service.Events.List(“public google calendar ID”);
          request.TimeMin = DateTime.Now;
          request.TimeMax = DateTime.Now.AddDays(1)
          request.SingleEvents = true;
          request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;

          Events events = request.Execute();

  • LATA JAGWANI

    Greetings Linda…!!

    I just want to ask is it possible to get all the events of google calendar and transfer into the excel file for future use..!!

    Thanks & Regards,
    Lata

    • Linda Lawton Post author

      I dont see any reason why you couldn’t do that. Events.list will return all of the events in Google Calendar so writing them to an Excel file shouldn’t be to hard.

    • Linda Lawton Post author

      There is no way to get a list of them. If I was you i would make my own hash table with the country code as the key and then the value the link to the different calendars you can then store this in your application.

  • Michael

    Linda Does this still work? I’m getting a Not found 404 error when I compile this with my public calendar:

    An unhandled exception of type ‘Google.GoogleApiException’ occurred in mscorlib.dll
    Google.Apis.Requests.RequestError
    Not Found [404]
    Errors [
    Message[Not Found] Location[ – ] Reason[notFound] Domain[global]
    ]

    The Application name is the Name in the Google Console Under credentials right before the key correct?

    • Linda Lawton Post author

      Application name isn’t really used for anything other then saving credentials. Api key is what is important here. Not found almost sounds like you have given the wrong name of the calendar.

  • Remisha

    Hi Linda,
    Great Tutorial !! This helped me to get Public Holidays for 3 years (previous year, current year, next year).
    I have a query, that is there a way to get the public holidays by year, I mean by passing year as parameter? or is there a way to get 5 years public holidays?