Enumerate Events on Public Calendar / C# / Google.Apis.Calendar.v3

923 views
Skip to first unread message

AB

unread,
Sep 22, 2014, 1:52:23 PM9/22/14
to google-ca...@googlegroups.com
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.

This is what I have so far, it's essentially me trying to rewrite the VB example (https://code.google.com/p/google-api-dotnet-client/source/browse/Calendar.VB.ConsoleApp/Program.vb?repo=samples):

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;
       
}
   
}
}



AB

unread,
Sep 22, 2014, 2:46:05 PM9/22/14
to google-ca...@googlegroups.com
I changed it to use an API key credential instead, and that appears to be working so far.  New Code:

    public class SimpleEvent
   
{
       
public string Title { get; set; }

       
public string Description { get; set; }

       
public DateTime? Start { get; set; }
       
public DateTime? End { 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);



               
var initializer = new BaseClientService.Initializer();
                initializer
.ApiKey = "...my api key...";

                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,
                       
Title = item.Summary,
                       
Start = item.Start.DateTime,
                       
End = item.End.DateTime
Reply all
Reply to author
Forward
0 new messages