GigaKatowice
GigaKatowice

Reputation: 551

Google API v3 - can't get list of calendars

I'm trying to add events to my calendar.

I created service account credentials in Google Developers Console and I can authorize myself.

I have two calendars on my account, but I can't list them using c# code to google API.

Do I need some special permissions to my calendars? On settings (calendar.google.com) tab I have full permissions. Maybe there is something wrong with code which gets the calendar list?

 string[] scopesCal = new string[] { CalendarService.Scope.Calendar, // Manage your calendars
                                              CalendarService.Scope.CalendarReadonly, // View your Calendars
                                             };

         var certificate = new X509Certificate2( @"GigaNetLibGoogleXCalendar.p12", "notasecret", X509KeyStorageFlags.Exportable );

         ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer( ServiceAccountEmail ) {
               Scopes = scopesCal
            }.FromCertificate( certificate ) );

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

         var calendars = CalSrv.CalendarList.List().Execute().Items;
         foreach ( CalendarListEntry entry in calendars )
            Console.WriteLine( entry.Summary + " - " + entry.Id );

Upvotes: 3

Views: 2998

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117281

You are using a service account by default a service account doesn't have any Google calendars. You either need to add one or give it access to one of yours.

Take the service account email address and add it as a user on the calendar on your account you want it to be able to access.

Go to the Google Calendar website. Find the Calendar Settings , then go to the Calendars tab, find the calendar you want to access and click on “Shared: Edit settings” add the service account email address like you would a persons email address. This will give the service account the same access as if you where sharing it with any other user.

You will probably want to give it permission "make changes to events"

Tip

you only need the CalendarService.Scope.Calendar that will give it full access. You can remove the CalendarService.Scope.CalendarReadonly there is really no reason to have both.

Upvotes: 3

Related Questions