【发布时间】:2013-12-16 23:12:55
【问题描述】:
我正在尝试编写一个程序来同步多个 Google 帐户的 CRM 和 Google 日历之间的事件。
这是一个 Windows 控制台应用程序。
对于每个 Google 帐户,我在 API 控制台中启用了日历 API,创建了一个名为“UpdateSync”(类型:本机)的应用程序,然后下载了 JSON 文件。
我使用帐户邮件地址(即 email1@gmail.com.json)重命名了这些文件
接下来,我写了下面的代码:
/// <summary>
/// Loads updated event from Google Calender after last synchonization date
/// </summary>
/// <param name="user">The user we want to get appointments</param>
public List<Appointment> LoadAppointments(User user)
{
Console.WriteLine("LoadAppointments({0})", user.Email);
List<Appointment> Appointments = new List<Appointment>();
UserCredential credential;
using (var stream = new FileStream(string.Format("credentials\\{0}.json", user.Email), FileMode.Open, FileAccess.Read))
{
try
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, new[] { CalendarService.Scope.Calendar }, "user", CancellationToken.None, new FileDataStore("UpdayeSync.Credentials")).Result;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
BaseClientService.Initializer initializer = new BaseClientService.Initializer();
initializer.HttpClientInitializer = credential;
initializer.ApplicationName = "UpdateSync";
service = new CalendarService(initializer);
EventsResource.ListRequest req = service.Events.List("primary");
req.TimeMin = DateTime.Now.AddMonths(-2).ToString("o");
req.ShowDeleted = true;
req.UpdatedMin = XmlConvert.ToString(LastSync, XmlDateTimeSerializationMode.Utc);
req.SingleEvents = true;
Events events;
try
{
events = req.Execute();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
// [...]
}
为每个 JSON 文件调用此代码。
第一次通话时,要同步的帐户是“email1@gmail.com”。 谷歌浏览器自动打开,并使用我自己的帐户 (email2@gmail.com) 自动登录,并要求我允许我的程序访问日历 API。
我接受了。
这个弹出窗口再也没有出现过。
对任何帐户所做的任何更改都是在我自己的日历上完成的,而不是 JSON 文件引用的那个。
怎么了?如何使用独特的程序访问多个 Google 帐户?
这个程序将作为计划任务在服务器上运行,我不能打开任何窗口,因为没有人会看服务器的屏幕。由于没有用户,我无法要求用户进行身份验证。
但我可以对任何用户的 Google 帐户执行任何操作(启用 API、创建密钥、授予应用程序等)
【问题讨论】:
标签: calendar google-api-dotnet-client