Holidays with the Google calendar api and C#.


.net core

Do you have an application that needs to display holidays to your users? Are you having issues finding holidays for different countries. Well this post is going to help you do that.

Today we are going to see how to request holidays from the Google Calendar API using an api key using C#. An Api key is a unique identifier required by Google when making calls to their APIs.

Creating an api key

Api keys are used by Google to identify your application, they are mainly used when accessing public data. In order to follow along with this tutorial you will need your own api key.

If you would like to learn more about API keys as well as how to create your own please check my other blog post on Google developer console API keys or the companion video for that post How to create an api key, fast!

Calendar id

This method will work with any google calendar which has been set to public, in this example we will be using one of the google calendar holiday calendars. You will need to go to the Google calendar web app and find the calendar id in the settings for the calendar you wish to get the events for.

if you are having issues in finding the calendar id you can check the companion video for this post which shows you exactly how to find it

Code

The first thing we need to do is include the nuget package Google.Apis.Calendar.v3

Install-Package Google.Apis.Calendar.v3

Our code will have two constants the first being the API key and the second being our calendar Id. Once we have added that we will create a CalendarService all requests to the google calendar api go though the CalendarService. Notice how i have passed it our API key.

Once we have a calendar serice acreated we can then call the endpoint for the Google calendar event list method We need to pass it the calendar id of the calendar we want to find events from.

I have also added the optional parameter of fields here to limit the response to only the summary, start and end dates for our events.


 class Program
    {
        const string ApiKey = "AIzaSyAaXxI53pCVT7WAGqC3ZzGWndrvuha80eQ";
        const string CalendarId = "en.danish#holiday@group.v.calendar.google.com";

        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var service = new CalendarService(new BaseClientService.Initializer()
            {
                ApiKey = ApiKey,
                ApplicationName = "Api key example"
            });

            var request = service.Events.List(CalendarId);
            request.Fields = "items(summary,start,end)";
            var response = await request.ExecuteAsync();

            foreach (var item in response.Items)
            {
                Console.WriteLine($"Holiday: {item.Summary} start: {item.Start} end: {item.End}");
            }

            Console.ReadLine();
        }
    }

Notice how the response contains a list of Items, each item is our holiday event.

Video

A companion video for this post can be found here

Conclusion

Google calendar offers a number of public calendars that we can access, as they are public we don’t need to use Oauth2 in order to authorize a user we can use a public api key. Public api keys give us read only access, they will not allow us to write to the calendar even if the calendar is set to public.


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.