Brief:

Google calendar API is a Google API used to interact with the Google Calendar and can be used to retrieve calendar information as well as create/update/delete calendar events and much more.

Getting Started:

Step 1: Go to Google Developer Console

Step 2: Select or create a project

Step 3: In the left hand side bar, select API & Auth then APIs and activate Calendar API.

Step 4: Select credentials and click on create new client Id, and then select application type as Web Application and click on Create Client Id

Now we are ready to work on Google Calendar API. Note down the client Id and Client secret as this will be required while using the Google Calendar API.

Retrieve Calendar Information

Step 1: Create a C# project and install Google Calendar API using package manager.

 Step 2: Initialize the calendar service as bellow

CalendarService service;
string CTFolderPath = AppDomain.CurrentDomain.BaseDirectory + @"CALENDAR";
#region Get the refresh token value
string refreshToken = string.Empty;
DirectoryInfo directory = new DirectoryInfo(CTFolderPath);
if (directory.GetFiles().Count() > 0)
{
   string filename = directory.GetFiles().OrderByDescending(f =>                                                                                                                                              f.LastWriteTime).FirstOrDefault().FullName;
   string jsonText = File.ReadAllText(filename);
   var test = (GoogleToken)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonText,       typeof(GoogleToken));
    refreshToken = test.refresh_token;//tokenOjbect["refresh_token"].ToString();
 }
#endregion
scopes.Add(CalendarService.Scope.Calendar);
UserCredential credential = default(UserCredential);
FileDataStore _fdsToken = new FileDataStore(CTFolderPath);
 
//Client Id generated while setting up the calendar API in Google Developer Console
//Client Secret generated while setting up the calendar API in Google Developer Console
 
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets { ClientId = "xxxxxxxxxxxxxxxxx", 
                      ClientSecret = "yyyyyyyyyyyyyyyyyy",
                  }, scopes,
                  "Z",
CancellationToken.None, _fdsToken).Result;
credential.Token.ExpiresInSeconds = 500000;
String token = credential.Token.RefreshToken;
credential.Token.RefreshToken = refreshToken;
credential.Token.ExpiresInSeconds = 500000;
 
// Create the calendar service using an initializer instance
BaseClientService.Initializer initializer = new BaseClientService.Initializer();
initializer.HttpClientInitializer = credential;
initializer.ApplicationName = "Google Calendar"; // Name for the application
service = new CalendarService(initializer);

Remember for the first time this CALENDAR folder won’t be present, when you run this method for the first time, it will open a Google login page to allow the access to the Google calendar as shown in the screenshot and after allowing the access this “CALENDAR” older will be created and inside a file with a JSon text will be created.

Google calender Working with Google Calendar API using C# – Part I

Now the calendar service is initialized and can be used to retrieve calendar and event information and create/update/delete any event from Google calendar.

Here is a sample example,

// Retrieve all events from the calendar

var events = service.Events.List(“Calendar Id”)

//Create event in Google calendar

Event evnt = new Event();

var returnedEventDetailes = service.Events.Insert(evnt, “Calendar Id”).Execute();

In the next part of this blog, I will write in detail about the insert/update/delete operations in Google calendar

By |December 31st, 2014|Technology|0 Comments

About the Author:

 Working with Google Calendar API using C# – Part I
Working as a Technical Leader at e-Zest Solutions Limited with over 6 Years of Experience in .Net technologies, with expertise in WPF and WCF.

This entry passed through the Full-Text RSS service – if this is your content and you’re reading it on someone else’s site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.

e-Zest | India | USA | UK | Germany | Europe