1
votes

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 = "[email protected]";

            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();                
2
I have been struggling with Google .NET library as well. The API seems good but it as a few problems 1) Every release seems to totally break the last version and 2) There are no good sample applications, just code snippets that dev's need to jigsaw together. - Craig
1. There were broken changes while the library was BETA. But, now that had reached GA there won't be any incompatible changes. 2. There are few samples in our samples repository - code.google.com/p/google-api-dotnet-client/source/…. Remember that this is an open source project so it will be great if you will share a sample in the future. - peleyal
Not many breaking changes since beta, but many breaking changes since version 1.2. - Craig

2 Answers

0
votes

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).

0
votes

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 = "[email protected]",
        Scopes = new[] { CalendarService.Scope.Calendar }
    }.FromCertificate(certificate));

// ... Define the event    

service.Events.Insert(e, "primary").Execute();