Share via


Using Google Calendar Api V.3 in C#

Question

Wednesday, July 16, 2014 2:39 PM

hi there,

I have followed all the instruction provided by google to use Google Calendar Api in my application but my fails with nulls.

1. Create a Google Console account.
2. Activate the api you want to use in your application
3. Generate Outh 2. version client ID and secret key  for installed application
4. Installed the required nuget packages.

static IList<string> scopes = new List<string>();
        static CalendarService service;
        public static void Main()
        {
           
            scopes.Add(CalendarService.Scope.Calendar);

           
            Console.WriteLine("Google.Apis.Calendar.v3 Sample");
            Console.WriteLine("==============================");

            UserCredential credential = default(UserCredential);
           
          credential =  GoogleWebAuthorizationBroker.AuthorizeAsync(
          new ClientSecrets
          {
              ClientId = "myid",
              ClientSecret = "mysecret"
          },
          new[] { CalendarService.Scope.Calendar },
          "@gmail.com",
          CancellationToken.None,
          new FileDataStore(@"G:\CalendarCsharp")).Result;
                       
            BaseClientService.Initializer initializer = new BaseClientService.Initializer();
            initializer.HttpClientInitializer = credential;
            initializer.ApplicationName = "CalendarSample";
            service = new CalendarService(initializer);
                        
            IList<CalendarListEntry> list = service.CalendarList.List().Execute().Items;

            DisplayList(list);
            foreach (Google.Apis.Calendar.v3.Data.CalendarListEntry calendar in list)
            {
                
                DisplayFirstCalendarEvents(calendar);
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
              
        private static void DisplayList(IList<CalendarListEntry> list)
        {
            Console.WriteLine("Lists of calendars:");
            foreach (CalendarListEntry item in list)
            {
                Console.WriteLine(item.Summary + ". Location: " + item.Location + ", TimeZone: " + item.TimeZone);
            }
        }

by above program fails with System.AggregateException and points to the following line of codes :

credential =  GoogleWebAuthorizationBroker.AuthorizeAsync(
          new ClientSecrets
          {
              ClientId = "myid",
              ClientSecret = "myscret"
          },
          new[] { CalendarService.Scope.Calendar },
          "gmail.com",
          CancellationToken.None,
          new FileDataStore(@"G:\CalendarCsharp")).Result;

please tell me where am I going wrong. ? :(

All replies (5)

Wednesday, July 16, 2014 9:27 PM âś…Answered

Hello,

I'd ask Google:

https://groups.google.com/forum/embed/?place=forum/google-calendar-api&showsearch=true#!forum/google-calendar-api

karl

When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
My Blog: Unlock PowerShell
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ('6F6C646B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})


Thursday, July 17, 2014 1:38 PM

thanks for your response. :)


Wednesday, November 19, 2014 6:24 AM

Following program must work:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using Google.Apis.Auth.OAuth4;
using Google.Apis.Util.Store;

using Google;

namespace GoogleCalendar
{
    class Program
    {
        static CalendarService service;

        static void Main(string[] args)
        {
            UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets
                {
                    ClientId = "your_id",
                    ClientSecret = "your_secret",
                },
                new[] { CalendarService.Scope.Calendar },
                "user",
                CancellationToken.None).Result;

            // Create the service.
            service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Calendar API Sample",
            });

            // Fetch the list of calendar list
            IList<CalendarListEntry> list = service.CalendarList.List().Execute().Items;

            // Display all calendars
            DisplayList(list);

            foreach (CalendarListEntry calendar in list)
            {
                // Display calendar's events
                DisplayFirstCalendarEvents(calendar);
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        // Displays all calendars
        private static void DisplayList(IList<CalendarListEntry> list)
        {
            Console.WriteLine("Lists of calendars:");
            foreach (CalendarListEntry item in list)
            {
                Console.WriteLine("Summary: " + item.Summary);
                Console.WriteLine("Location:" + item.Location);
                Console.WriteLine("TimeZone:" + item.TimeZone);
            }
        }

        // Displays the calendar's events
        private static void DisplayFirstCalendarEvents(CalendarListEntry list)
        {
            Console.WriteLine(Environment.NewLine + "Maximum 5 first events from {0}:", list.Summary);
            EventsResource.ListRequest requeust = service.Events.List(list.Id);

            //Set MaxResults and TimeMin with sample values
            requeust.MaxResults = 5;
            requeust.TimeMin = new DateTime(2013, 10, 1, 20, 0, 0);

            // Fetch the list of events
            foreach (Event calendarEvent in requeust.Execute().Items)
            {
                Console.WriteLine("Summary: " + calendarEvent.Summary);
            }
        }
    }
}


Wednesday, November 26, 2014 8:58 PM

hi, i have a simple question about use of new library v3,

i tried this code and work but i can access at "my calendar", i have the application that must access to calendar of client's that use it.

with previously library was very simple, but now i dont understand how can i access to calendar of end-user (with their user and psw) after the first step (autentication with secret key created with the project .....)

if i have many clients i cannot ask them of create and save the key for receive the token, i think that get the token with my key and after with user and psw of end-users can access to their calendar

its possible have some example for this?

thanx


Friday, February 27, 2015 10:01 AM

hi, i have a simple question about use of new library v3,

i tried this code and work but i can access at "my calendar", i have the application that must access to calendar of client's that use it.

with previously library was very simple, but now i dont understand how can i access to calendar of end-user (with their user and psw) after the first step (autentication with secret key created with the project .....)

if i have many clients i cannot ask them of create and save the key for receive the token, i think that get the token with my key and after with user and psw of end-users can access to their calendar

its possible have some example for this?

thanx

You can use a FileDataStore:

using Google.Apis.Auth.OAuth4;
using Google.Apis.Util.Store;
using System.Threading;
using System.Threading.Tasks;

ClientSecrets cs = new ClientSecrets
{
    ClientId = "your clientId",
        ClientSecret = "your clientSecret"
};

FileDataStore store = new FileDataStore("path of temporary dir", true);
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(cs, scopes, gUser, CancellationToken.None, store).Result;