谷歌API V3 - 无法获取日历列表

3

我想在我的日历中添加事件。

我在Google开发者控制台创建了服务账户凭据,并成功进行了授权。

我在我的账户上有两个日历,但是我不能使用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 );
1个回答

3

默认情况下,您正在使用一个服务账户,该服务账户没有任何Google日历。 您需要添加一个或让它访问您的一个。

将服务账户电子邮件地址添加为您要让其访问的帐户上的日历用户。

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

您可能需要授予它“更改事件”的权限“make changes to events”

提示

您只需要CalendarService.Scope.Calendar即可获得完全访问权限。 您可以删除CalendarService.Scope.CalendarReadonly,因为没有必要同时具有两者。


非常感谢。它帮助了我!我没有想到我需要为服务帐户添加权限。我以为为创建API凭据的帐户授予权限就足够了。再次感谢! - GigaKatowice
一个服务账户是一个虚拟用户,它没有访问任何你不给它访问权限的东西。但它有自己的谷歌网盘、谷歌日历...可能还有一堆其他账户,但它们一开始都没有任何数据。 - Linda Lawton - DaImTo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接