Google 带有 G Suite 帐户插入的日历得到 403

Google Calendar with G Suite account insert get 403

我需要使用 G Suite 帐户来插入日历,包括环聊会议 但我什至无法插入事件,我总是收到 403 响应

403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "calendar",
    "message" : "Service accounts cannot invite attendees without Domain-Wide Delegation of Authority.",
    "reason" : "forbiddenForServiceAccounts"
  } ],
  "message" : "Service accounts cannot invite attendees without Domain-Wide Delegation of Authority."
}

我使用 GCP p12 文件和服务帐户来做日历。

我还单击启用 G Suite domain-wide 委派框并添加我的 clientId 和范围

https://www.googleapis.com/auth/admin.directory.resource.calendar, https://www.googleapis.com/auth/calendar.events

在 G Suite 管理控制台

可能是什么问题!?

顺便问一下,我需要设置 OAuth 同意屏幕吗!?我已经保存了,但是没有被 google 批准。

有人能帮忙吗!!

一开始我通过以下代码获取凭证

credentials = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(JSON_FACTORY)
                    .setServiceAccountId(CalendarEntity.CALENDARID)
                    .setServiceAccountPrivateKeyFromP12File(new File(P12FILEPATH))
                    .setServiceAccountScopes(Collections.singleton(CalendarScopes.CALENDAR)).build();

然后我添加(我的 G Suite 帐户)

.setServiceAccountUser("xxx@xxx.com.tw") 

它回应 401未经授权 我怎么能爱这个!?谢谢

您缺少模拟。

向服务帐户授予全域权限的目的是让这些帐户能够代表域中的 用户访问数据

如果您授予它域范围的权限但不是 "impersonating" 任何帐户,服务帐户就好像您没有授予此权限一样:它正在尝试访问自己的日历、云端硬盘文件等,或者在这种情况下,尝试插入一个事件:服务帐户 cannot currently do 的东西,我想你已经知道了。

当服务帐户模拟域中的另一个用户时(即当它代表用户行事时),服务帐户可以访问该用户可以访问的资源。仅此而已。委派之所以有用,是因为它可以对域中的任何用户执行此操作。

要冒充其他用户,您必须指定该用户的电子邮件地址。我不知道你使用的是哪个库,如果有的话,但是here you can see how to impersonate another user with Python, Java, and plain HTTP/REST. Refer to 如果你需要在Node.js库中进行。如果您使用的是其他库,请在库文档中寻找对应的方法。

参考: