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 );
您正在使用服务帐户默认情况下,服务帐户没有任何Google日历。 您要么需要添加一个,要么让它访问其中一个。
获取服务帐户电子邮件地址,并在您希望它能访问的帐户上的日历上作为用户添加。访问Google Calendar网站。查找日历设置,然后转到“日历”选项卡,查找要访问的日历,然后单击“共享:编辑设置”添加服务帐户的电子邮件地址,就像您的电子邮件地址一样。这将使服务帐户与您与任何其他用户共享的访问权限相同。 您可能想授予它许可
"make changes to events"
您只需要可以完全访问它的
CalendarService.Scope.Calendar
。 您可以删除
CalendarService.Scope.CalendarReadonly
确实没有理由拥有两者。
update -2025service帐户仅与Google Workspace帐户中的域名委员会合作使用Google日历。他们无法使用标准的Gmail用户。