The program failed with two inner exceptions and I had no idea how to catch the inner exception. First I figured out how to catch the inner exceptions using this code :
try
{
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("ERROR: " + e.Message+" "+e.StackTrace);
}
}
First Exception : The program was throwing a clumsy Thread Exception. To fix this issue, I had to install a latest version of a package for Microsoft Async 1.0.166 using nuget.
Second Exception : The program was not able to find FileDataStore Method, To fix this I changed my previous code to this one :
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { CalendarService.Scope.Calendar },
"user", CancellationToken.None);
}
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "CalendarApp",
});
await DisplayList(service);
await credential.RefreshTokenAsync(CancellationToken.None);
try
{
await DisplayList(service);
}
catch (TokenResponseException ex)
{
Console.WriteLine(ex.Error);
}
await GoogleWebAuthorizationBroker.ReauthorizeAsync(credential, CancellationToken.None);
await DisplayList(service);
}
private async Task DisplayList(CalendarService service)
{
var list = await service.CalendarList.List().ExecuteAsync();
if(list.Items==null)
{
Console.WriteLine("There is no Calendar");
return;
}
Console.WriteLine("Lists of calendars:");
foreach (CalendarListEntry item in list.Items)
{
Console.WriteLine("Item:"+item.Location+" "+item.Summary);
}
}
that's all.