使用服务帐户身份验证访问Google Calendar API [英] Access Google Calendar API using Service Account Authentication
问题描述
我可以使用 .NET Quickstart
访问Google Calendar API。 a> tutorial,它工作的很好!该教程的问题在于它使用开放认证或OAuth2
。我希望使用服务帐户身份验证执行相同操作。
( https:/ /support.google.com/googleapi/answer/6158857?hl=zh_CN )
有人可以举例说明我如何使用我的日历一个服务帐户密钥文件?
我曾尝试使用 Google Calendar API Authentication with C#
记住服务帐户不是你。该服务帐户在Google日历帐户中有该帐户,因此如果您想阅读您的某个个人日历,则需要与该服务帐户分享。
此处是另一个使用 Json服务帐户密钥文件的示例。
string [] scopes = new string [] {CalendarService.Scope.Calendar};
GoogleCredential凭证;
using(var stream = new FileStream(serviceAccountCredentialFilePath,FileMode.Open,FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes);
}
//创建日历服务。
var service new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer =凭证,
ApplicationName =Calendar Authentication Sample,
});
I was able to access the Google Calendar API using the .NET Quickstart tutorial and it worked great!
The problem with that tutorial is that it uses Open Authentication or OAuth2
. I will like to do the same using Service Account Authentication.
(https://support.google.com/googleapi/answer/6158857?hl=en)
Can someone give me an example of how I can access my calendar using a Service account key file?
I have tried also tried using Google Calendar API Authentication with C# tutorial and was not able to accomplish it.
I am curious as to why your first attempt with the service account tutorial didn't work. What was wrong? Was there an error?
Remember service accounts are not you. The service account has its on Google calendar account so if you are trying to read one of your personal calendars you are going to have to share it with the service account.
Here is another example using the Json service account key file.
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",
});
这篇关于使用服务帐户身份验证访问Google Calendar API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!