Question

I'm using Google Calendar API V3, with OAuth2 & .NET. my authentication is by Service Account, since i need to run it as a service, without user interface. I've managed, after a lot of struggle to authenticate with the credentials, but for some reason i can't create an event on my calendar (& yes, i shared it with my self...).

i found a lot of questions regarding some same issues, but all in php, which i don't really know or think it will help me.

i seek for some help. Thanks

    String serviceAccountEmail = "XXX@developer.gserviceaccount.com";

            var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.MachineKeySet |X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
            ServiceAccountCredential credential = new ServiceAccountCredential(
               new ServiceAccountCredential.Initializer(serviceAccountEmail)
               {
                   Scopes = new[] { CalendarService.Scope.Calendar }
               }.FromCertificate(certificate));

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

            Event e = new Event()
            {
                Summary = "Appointment1",
                Location = "42.138679, -88.045519",
                Start = new EventDateTime()
                {
                    DateTime = DateTime.Now,
                    TimeZone = "Asia/Jerusalem"
                },
                End = new EventDateTime()
                {
                    DateTime = DateTime.Now.AddDays(1),
                    TimeZone = "Asia/Jerusalem"
                },
            };

            Event createdEvent = service.Events.Insert(e, "primary").Execute();                
Was it helpful?

Solution

For some reason, the Insert event to a "primary" calendar, didn't do the job. Instead, i wrote the following code, which allowed me to write the event.

var list = service.CalendarList.List().Execute().Items;
service.Events.Insert(e, list[0].Id).Execute();

This is my solution for this problem, I also agree with Craig here that the API is not well organized. (saying this after working with the amazing API of maps).

OTHER TIPS

Perhaps try specifying the user programmatically, rather than sharing the calendar with your service account e-mail? The following worked in my application:

ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
        User = "youremail@yourdomain.com",
        Scopes = new[] { CalendarService.Scope.Calendar }
    }.FromCertificate(certificate));

// ... Define the event    

service.Events.Insert(e, "primary").Execute();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top