我正在尝试通过服务帐户连接到我们 G Suite 域上的所有日历。
我创建了一个新项目,并在 API 和服务页面中启用了 Google Calendar API。
我创建了一个 Google 服务帐户:
- A name
- 似乎适用的角色
- 生成的json密钥文件
- 为整个域启用 G Suite
在服务帐户管理页面中,我获取了我的 ID([电子邮件受保护] /cdn-cgi/l/email-protection)
我进入 G Suite 安全模块并转到 ManageOauthClients 页面,在其中输入 ID 并选择 google calendar API (https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar)
它正确识别了我的 ID 并添加了:
1234567890 日历(读写)https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar
其中 1234567890 是 Google 从我那里获取的 ID[电子邮件受保护] /cdn-cgi/l/email-protection input.
现在,我添加了 Google 的 nuget 包和日历 API,并在 C# 命令行应用程序中编写了以下代码:
var scopes = new[] { CalendarService.Scope.Calendar };
ServiceAccountCredential credential;
using (Stream stream = new FileStream("ToprServiceAccount-xxxxyyyyy.json", FileMode.Open, FileAccess.Read, FileShare.Read))
{
credential = GoogleCredential.FromStream(stream).CreateScoped(scopes).UnderlyingCredential as ServiceAccountCredential;
}
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
var list = service.CalendarList.Get("[email protected] /cdn-cgi/l/email-protection").Execute();
我确保我要求的日历存在。这是我用来实际执行上述所有步骤的帐户中的一个。
但我仍然收到 404 错误,这意味着我很可能没有访问(我自己的)日历的权限。
会不会跟角色有关?我不确定创建服务帐户时要选择什么角色。
此外,当只是尝试获取所有日历的列表时,它是一个空列表。