I am trying to do what I assume should be very simple, I need to read through events for public calendars and return them to a calling application in the form of a list of "SimpleEvent" objects.
This code appears to almost work, but I get an error back at this line "GoogleWebAuthorizationBroker.AuthorizeAsync(..)": A first chance exception of type 'System.InvalidOperationException' occurred in Google.Apis.Auth.dll Additional information: At least one client secrets (Installed or Web) should be set
What is that error trying to tell me?
I have created a "Service Account" in the Google Developer's Console (this will essentially be running as an overnight service to aggregate many calendars), and the credentials for that are stored in the "clientsecrets.json" file that is used in the filestream.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using System.IO;
using System.Threading;
public class SimpleEvent
{
public string Description { get; set; }
}
public class Google
{
public static List<SimpleEvent> Get(string calendarid)
{
try
{
List<SimpleEvent> myResults = new List<SimpleEvent>();
var scopes = new List<string>();
scopes.Add(CalendarService.Scope.Calendar);
UserCredential credential;
using(FileStream fs = File.OpenRead("clientsecrets.json"))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(fs).Secrets, scopes, "user", CancellationToken.None, new FileDataStore("My.Calendar.Application")).Result;
}
var initializer = new BaseClientService.Initializer();
initializer.HttpClientInitializer = credential;
initializer.ApplicationName = "Calendar API Test";
var service = new CalendarService(initializer);
var events = service.Events.List(calendarid);
foreach (var item in events.Execute().Items)
{
myResults.Add(new SimpleEvent { Description = item.Description });
}
return myResults;
}
catch (Exception)
{
throw;
}
}
}