【问题标题】:Google API v3 - can't get list of calendarsGoogle API v3 - 无法获取日历列表
【发布时间】:2015-11-02 09:02:43
【问题描述】:

我正在尝试将事件添加到我的日历中。

我在 Google Developers Console 中创建了服务帐户凭据,我可以自行授权。

我的帐户上有两个日历,但我无法使用 c# 代码将它们列出到 google API。

我的日历需要一些特殊权限吗? 在设置 (calendar.google.com) 选项卡上,我拥有完全权限。 获取日历列表的代码可能有问题?

 string[] scopesCal = new string[] { CalendarService.Scope.Calendar, // Manage your calendars
                                              CalendarService.Scope.CalendarReadonly, // View your Calendars
                                             };

         var certificate = new X509Certificate2( @"GigaNetLibGoogleXCalendar.p12", "notasecret", X509KeyStorageFlags.Exportable );

         ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer( ServiceAccountEmail ) {
               Scopes = scopesCal
            }.FromCertificate( certificate ) );

         // Create the service.
         CalSrv = new CalendarService( new BaseClientService.Initializer() {
            HttpClientInitializer = credential,
            ApplicationName = "XCalendar",
         } );

         var calendars = CalSrv.CalendarList.List().Execute().Items;
         foreach ( CalendarListEntry entry in calendars )
            Console.WriteLine( entry.Summary + " - " + entry.Id );

【问题讨论】:

    标签: c# google-api google-calendar-api google-api-dotnet-client service-accounts


    【解决方案1】:

    默认情况下,您使用的是服务帐户,服务帐户没有任何 Google 日历。您要么需要添加一个,要么授予它访问您的其中一个的权限。

    获取服务帐户电子邮件地址,并将其作为用户添加到您希望它能够访问的帐户的日历上。

    访问 Google 日历网站。找到日历设置,然后转到日历选项卡,找到您要访问的日历,然后单击“共享:编辑设置”添加服务帐户电子邮件地址,就像您添加个人电子邮件地址一样。这将为服务帐户提供与您与任何其他用户共享该服务帐户相同的访问权限。

    你可能想给它权限"make changes to events"

    提示

    您只需要CalendarService.Scope.Calendar 即可为其提供完全访问权限。您可以删除CalendarService.Scope.CalendarReadonly 确实没有理由同时拥有两者。

    【讨论】:

    • 非常感谢。它有帮助!我不认为我必须为服务帐户添加权限。我认为创建 API 凭据的帐户的权限就足够了。再次感谢!
    • 服务帐户是一个虚拟用户,它无权访问您未授予它访问权限的任何内容。但它确实有自己的谷歌驱动器、谷歌日历......可能还有一堆其他帐户,但他们一开始就没有任何数据。
    猜你喜欢
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    相关资源
    最近更新 更多