检索日历信息失败,返回null? [英] Retrieving Calendar information fails with null?
问题描述
我有这个代码从Goolge Calendar Api v.3获取日历信息。我已经成功创建了Google控制台帐户,并生成了客户端ID和密码。
当我运行此代码时,它失败并返回空值。异常:System.ObjectDisposedException未处理。使用以下代码行:
Hi, I have this code for getting Calendar information from Goolge Calendar Api v.3. and I have already created Google console account successfully and generated a client id and secret code.
when I run this code, it fails with null values. Exception : System.ObjectDisposedException was unhandled. with this lines of code:
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "id",
ClientSecret = "secret"
},
new[] { CalendarService.Scope.Calendar },
"user",
CancellationToken.None,
new FileDataStore(@"G:\CalendarCsharp")).Result;
static class Program
{
static IList<string> scopes = new List<string>();
static CalendarService service;
public static void Main()
{
scopes.Add(CalendarService.Scope.Calendar);
Console.WriteLine("Google.Apis.Calendar.v3");
Console.WriteLine("==============================");
UserCredential credential = default(UserCredential);
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "id",
ClientSecret = "secretcode"
},
new[] { CalendarService.Scope.Calendar },
"user",
CancellationToken.None,
new FileDataStore(@"G:\CalendarCsharp")).Result;
BaseClientService.Initializer initializer = new BaseClientService.Initializer();
initializer.HttpClientInitializer = credential;
initializer.ApplicationName = "CalendarSample";
service = new CalendarService(initializer);
IList<CalendarListEntry> list = service.CalendarList.List().Execute().Items;
DisplayList(list);
foreach (Google.Apis.Calendar.v3.Data.CalendarListEntry calendar in list)
{
DisplayFirstCalendarEvents(calendar);
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
private static void DisplayList(IList<CalendarListEntry> list)
{
Console.WriteLine("Lists of calendars:");
foreach (CalendarListEntry item in list)
{
Console.WriteLine(item.Summary + ". Location: " + item.Location + ", TimeZone: " + item.TimeZone);
}
}
}
}
推荐答案
程序失败,有两个内部异常,我不知道如何捕获内部异常。首先,我想出如何使用此代码捕获内部异常:
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
{
// your code for getting Calendar values, events etc.
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("ERROR: " + e.Message+" "+e.StackTrace);
}
}
首例异常:该程序抛出了一个笨拙的线程异常。要解决此问题,我必须使用nuget为Microsoft Async 1.0.166安装最新版本的软件包。
第二例外:该程序无法找到FileDataStore方法,为了解决这个问题,我将之前的代码改为此代码:
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);
// Request should fail now - invalid grant.
try
{
await DisplayList(service);
}
catch (TokenResponseException ex)
{
Console.WriteLine(ex.Error);
}
// Reauthorize the user. A browser should be opened, and the user should enter his or her credential again.
await GoogleWebAuthorizationBroker.ReauthorizeAsync(credential, CancellationToken.None);
// The request should succeed now.
await DisplayList(service);
}
private async Task DisplayList(CalendarService service)
{
// Execute async.
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);
// await DisplayFirstCalendarEvents(item, service);
}
}
这就是全部。
that's all.
这篇关于检索日历信息失败,返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!