使用服务帐户身份验证访问Google Calendar API

Ton*_*Nam 4 c# google-calendar-api google-api google-api-dotnet-client service-accounts

我可以使用.NET快速入门教程访问Google Calendar API ,效果很好!

该教程的问题在于它使用Open Authentication or OAuth2.我想使用服务帐户身份验证执行相同操作.

(https://support.google.com/googleapi/answer/6158857?hl=en)

有人能举例说明如何使用服务帐户密钥文件访问我的日历吗?

我也试过尝试使用带有C#教程的Google Calendar API身份验证,但无法完成它.

DaI*_*mTo 6

我很好奇为什么你第一次尝试使用服务帐户教程不起作用.哪里错了?有错误吗?

记住服务帐户不是你.该服务帐户拥有自己的 Google日历帐户,因此,如果您尝试阅读其中一个"个人日历",则无法使用.您将不得不与服务帐户共享您的个人日历.

这是使用Json服务帐户密钥文件的另一个示例.

string[] scopes = new string[] { CalendarService.Scope.Calendar };
GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
{
    credential = GoogleCredential.FromStream(stream)
                     .CreateScoped(scopes);
}

// Create the Calendar service.
var service = new CalendarService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "Calendar Authentication Sample",
});
Run Code Online (Sandbox Code Playgroud)

  • 一件重要的事情。现在仅与服务帐户共享日历是不够的。您必须通过 CalendarList.insert(calendarId) 在服务帐户中接受它。请参阅[链接](https://issuetracker.google.com/issues/148804709) (3认同)
  • 这(以及你的博客文章)对我来说有很大的帮助。最后,我的应用程序使用服务帐户对 Google Calendar API 进行了身份验证。谢谢! (2认同)
  • 与服务帐户共享我的个人日历正是我需要做的。谢谢@JalalEl-Shaer! (2认同)