php - Google Calendar API 服务帐户错误
问题描述
我收到这个错误
{ "error":
{ "errors":
[
{ "domain": "calendar", "reason": "forbiddenForServiceAccounts", "message": "Service accounts cannot invite attendees without Domain-Wide Delegation of Authority." }
],
"code": 403,
"message": "Service accounts cannot invite attendees without Domain-Wide Delegation of Authority."
}
}
已经关注了这个 https://developers.google.com/admin-sdk/directory/v1/guides/delegation
我正在使用这个库并在 laravel 5.7 上运行它:https ://github.com/spatie/laravel-google-calendar
有什么办法可以解决这个问题。请帮忙。
解决方案
以下是完成这项工作的步骤:
在您的服务帐户中启用域范围委派
1 - 为您的服务帐户提供日历范围
- 转到https://admin.google.com/并使用 G Suite 帐户登录。
- 转到安全性-> API 控制-> 域范围委派
- 添加新 => 设置您的服务帐户的客户端 ID(只有数字的那个)
- 设置以下范围:https ://www.googleapis.com/auth/calendar、https://www.googleapis.com/auth/calendar.events、https://www.googleapis.com/auth/admin.directory .resource.calendar
2 - 您的用户需要具有角色服务帐户令牌创建者
- 然后转到https://console.cloud.google.com/iam-admin
- 选择您的项目并转到左侧菜单中的 IAM。
- 选择您将用于模拟的帐户
- 点击编辑
- 添加角色服务帐户令牌创建者
- 根据https://developers.google.com/admin-sdk/directory/v1/guides/delegation为帐户启用域委派,要启用 G Suite 域范围委派部分,请按照以下步骤操作。
3 - 在您将模拟的帐户中创建一个日历
服务帐户没有日历,因此您必须创建自己的日历
- 使用您想要拥有日历的电子邮件登录https://calendar.google.com/ (我使用了不同的帐户,与我要模拟的帐户不同,也许它可以使用模拟帐户中的日历)
- 创建日历
- 与有权修改和管理日历的服务帐户共享日历
- 与您将模拟的具有修改和管理日历权限的帐户共享日历
创建谷歌客户端
- 验证您的服务帐户。(我使用了 JSON 密钥,我不确定其他身份验证是否适用于此目的)
代码示例:(我使用了 PHP,但我假设其他语言非常相似,因此您可以将其用作指南)
请注意,使用一些电子邮件进行 IMPERSONALIZATION 是至关重要的。否则,403 错误将仍然存在,请使用它进行身份验证,有关详细信息,请参阅 Maksym Kalin 响应。
$google_client = new Google_Client();
$google_client->setAuthConfig($LOCATION_OF_JSON_KEY);
$google_client->setAccessType( 'offline' );
$google_client->setSubject('EmailToImpersonate@SomeAddress.com');
$google_client->setApplicationName("YourApplicationName");
$google_client->setScopes([\Google_Service_Calendar::CALENDAR, \Google_Service_Calendar::CALENDAR_EVENTS]);
与受邀者一起创建活动:) 并享受!
注意: 使用这种方法,您可以创建活动并邀请人们参加。请记住 G Suite https://support.google.com/a/answer/2905486的限制,因此如果您想创建许多活动,您将需要一个服务帐户池和一个日历池。
推荐阅读
- javascript - 在 javascript 中创建 JSON 嵌套数组元素
- python - 如何防止 lxml 删除 doctype
- grok - Telegraf grok patten 用于推送日志
- google-cloud-router - 云路由器限制
- azure-devops - 在 DevOps 管道中,如何在一个分析服务模型中处理多个数据源
- django - 从 Django 中的两个表中计算发票应用程序的总值
- amazon-web-services - 为 IAM 用户角色信任策略定义委托人时的困惑
- microsoft-graph-api - 用于使 OneNote 页面不起作用的 Microsoft 图形 API
- python-3.x - 在张量流数据集上应用地图执行非常缓慢
- swift - 在 Xcode 中,什么可以让程序使用断点,但不是没有?