使用服务帐户身份验证访问Google Calendar API [英] Access Google Calendar API using Service Account Authentication

查看:193
本文介绍了使用服务帐户身份验证访问Google Calendar API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 .NET Quickstart

访问Google Calendar API。 a> tutorial,它工作的很好!



该教程的问题在于它使用开放认证或OAuth2 。我希望使用服务帐户身份验证执行相同操作。

https:/ /support.google.com/googleapi/answer/6158857?hl=zh_CN



有人可以举例说明我如何使用我的日历一个服务帐户密钥文件?

我曾尝试使用

Google Calendar API Authentication with C# tutorial并且无法完成。 >我很好奇你为什么第一次尝试使用服务帐户教程不起作用。什么问题?是否有错误?



记住服务帐户不是你。该服务帐户在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆