简体   繁体   English

使用 Google 日历 API 和服务帐户创建事件

[英]Creating events using the Google Calendar API and Service Account

So I have setup my service account with domain wide delegation as described in this post Perform G Suite Domain-Wide Delegation of Authorit .因此,我已按照执行 G Suite Domain-Wide Delegation of Authorit这篇文章中的描述设置了我的服务帐户与域范围的委派。 In G Suite Admin Console I have authorized the service account to use the OAuth 2.0 scope:在 G Suite 管理控制台中,我已授权服务帐户使用 OAuth 2.0 scope:

https://www.googleapis.com/auth/calendar. 

Every time I try to create an event I end up with this error每次我尝试创建一个事件时,我都会遇到这个错误

GoogleJsonResponseException: 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."
}
new GoogleCredential.Builder()
      .setTransport(HTTP_TRANSPORT)
      .setJsonFactory(JSON_FACTORY)
      .setServiceAccountId(json.get("client_id").getAsString)
      .setServiceAccountPrivateKeyId(json.get("private_key_id").getAsString)
      .setServiceAccountPrivateKey(getPrivateKeyFromPkcs8(jsonCreds.get("private_key").getAsString))
      .setServiceAccountScopes(SCOPES)
      .build()

This building the google credential.这建立了谷歌凭证。 No impersonation is used, and the calendar is shared with the service account.不使用模拟,日历与服务帐户共享。

Service accounts are their own user.服务帐户是他们自己的用户。 They have their own Google calendar account.他们有自己的 Google 日历帐户。

If you want to give a service account access to another calendar there are two ways of doing that.如果您想让服务帐户访问另一个日历,有两种方法可以做到这一点。

  1. you could share the calendar with the Service account this will give it access to the calendar like any other user.您可以与服务帐户共享日历,这将使其像任何其他用户一样访问日历。 When creating events they will be created by the service account.创建事件时,它们将由服务帐户创建。

  2. If you have a gsuite account as you do then you can use domain wide delegation which will allow the service account to impersonate another user then when the service account creates events the event will be created by that user.如果您有一个 gsuite 帐户,那么您可以使用域范围的委派,这将允许服务帐户模拟另一个用户,然后当服务帐户创建事件时,该事件将由该用户创建。

I hope this helps to clear things up for you a bit.我希望这有助于为您解决一些问题。

Contacted G Suite support and any service accounts made after March 2, 2020 will no longer be able to invite guests to events without using impersonation.在 2020 年 3 月 2 日之后联系的 G Suite 支持和任何服务帐户将无法在不使用假冒的情况下邀请客人参加活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM