-2

I am able to retrieve event from google calendar API using OAuth authentication by code given here, but how to code for getting event information using API key.

Pritesh Chhunchha
  • 179
  • 1
  • 2
  • 15

1 Answers1

1

API key is for use with PUBLIC data. For an API key to work with a calendar I would assume said calendar would have to be set to public.

var service = new CalendarService(new BaseClientService.Initializer()
        {
         ApiKey = "XXX",
         ApplicationName = "xyz",

        });
var events = service.Events.List("en.danish#holiday@group.v.calendar.google.com").Execute();

foreach (var myEvent in events.Items)
     {
     Console.WriteLine(string.Format("Event: {0} Start: {1} End: {2}", myEvent.Summary, myEvent.Start.DateTime.ToString(), myEvent.End.DateTime.ToString()));
     }

I have an example / tutorial of accessing events on public calendars here

I think you should consider looking at a service account instead this will allow you to grant the service account access to your calendar and you wont need to go though the Oauth2 authentication which I suspect is what you don't want to be doing.

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

 var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(serviceAccountEmail) {
        Scopes = scopes
    }.FromCertificate(certificate));

I have a tutorial on how to us a service account here

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I have used service account with API version 3.0 when I try to get calendar by CalendarList calendars = request1.Execute(); it gives calendar count as 0. But id I shared service account email Id to my gmail account(xxxx@gmail.com) then I am getting the events information properly. – Pritesh Chhunchha Feb 16 '16 at 06:32