【问题标题】:Authentication with Google Calendar API using Server Account and .NET使用服务器帐户和 .NET 使用 Google Calendar API 进行身份验证
【发布时间】:2014-05-12 12:11:34
【问题描述】:

我正在使用带有 OAuth2 和 .NET 的 Google Calendar API V3。 我的身份验证是通过服务帐户进行的,因为我需要将其作为服务运行,而无需用户界面。 经过一番努力,我已经设法使用凭据进行身份验证,但由于某种原因,我无法在我的日历上创建活动(是的,我与自己分享了它......)。

我发现了很多关于一些相同问题的问题,但都是在 php 中,我真的不知道或认为它会对我有所帮助。

我寻求帮助。谢谢

    String serviceAccountEmail = "XXX@developer.gserviceaccount.com";

            var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.MachineKeySet |X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
            ServiceAccountCredential credential = new ServiceAccountCredential(
               new ServiceAccountCredential.Initializer(serviceAccountEmail)
               {
                   Scopes = new[] { CalendarService.Scope.Calendar }
               }.FromCertificate(certificate));

            // Create the service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {                    
                HttpClientInitializer = credential,
                ApplicationName = "Calendar API Sample",
            });

            Event e = new Event()
            {
                Summary = "Appointment1",
                Location = "42.138679, -88.045519",
                Start = new EventDateTime()
                {
                    DateTime = DateTime.Now,
                    TimeZone = "Asia/Jerusalem"
                },
                End = new EventDateTime()
                {
                    DateTime = DateTime.Now.AddDays(1),
                    TimeZone = "Asia/Jerusalem"
                },
            };

            Event createdEvent = service.Events.Insert(e, "primary").Execute();                

【问题讨论】:

  • 我也一直在努力使用 Google .NET 库。 API 看起来不错,但也有一些问题 1) 每个版本似乎都完全破坏了上一个版本;2) 没有好的示例应用程序,只有开发人员需要拼图的代码 sn-ps。
  • 1.在库是 BETA 时发生了一些损坏的更改。但是,现在已经达到 GA,不会有任何不兼容的更改。 2. 我们的示例存储库中的示例很少 - code.google.com/p/google-api-dotnet-client/source/…。请记住,这是一个开源项目,因此如果您将来能分享一个示例,那就太好了。
  • 自 Beta 版以来没有太多重大更改,但自 1.2 版以来发生了许多重大更改。

标签: google-calendar-api google-oauth google-api-dotnet-client


【解决方案1】:

由于某种原因,“主”日历的插入事件没有完成这项工作。 相反,我编写了以下代码,它允许我编写事件。

var list = service.CalendarList.List().Execute().Items;
service.Events.Insert(e, list[0].Id).Execute();

这是我对这个问题的解决方案,我也同意 Craig 的观点,即 API 组织得不好。 (在使用了令人惊叹的地图 API 之后这么说)。

【讨论】:

    【解决方案2】:

    也许尝试以编程方式指定用户,而不是与您的服务帐户电子邮件共享日历?以下在我的应用程序中有效:

    ServiceAccountCredential credential = new ServiceAccountCredential(
        new ServiceAccountCredential.Initializer(serviceAccountEmail)
        {
            User = "youremail@yourdomain.com",
            Scopes = new[] { CalendarService.Scope.Calendar }
        }.FromCertificate(certificate));
    
    // ... Define the event    
    
    service.Events.Insert(e, "primary").Execute();
    

    【讨论】:

      猜你喜欢
      • 2014-01-09
      • 2017-03-01
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 2013-09-23
      • 2019-08-25
      相关资源
      最近更新 更多