使用服务帐户创建 Google 日历活动时出错

问题描述 投票:0回答:3

我需要在日历上创建谷歌日历活动并将其他用户添加为该活动的参与者。目的是在未经应用程序用户同意的情况下将日历事件发送给他们(O-Auth)。

阅读Google的文档后,我发现我需要一个服务帐户。因此,我从 G-Suite 的一个电子邮件地址创建了一个项目和一个服务帐户,[电子邮件受保护],并为其启用了日历 API。

我创建并下载了一个密钥对(JSON),其内容是,

{
  "type": "service_account",
  "project_id": "*****",
  "private_key_id": "bdbbcd**************49f77d599f2",
  "private_key": "**"
  "client_email": "******@*****.iam.gserviceaccount.com",
  "client_id": "11083******576856",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/****dev%40*****kdev.iam.gserviceaccount.com"
}

根据文档,我继续编写身份验证流程代码,

public static GoogleCredential doOauth( String credsPath ) throws IOException
    {
        GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(credsPath))
                .createScoped(Collections.singleton(CalendarScopes.CALENDAR));
        System.out.println(credential);
        return credential;
    }

credential
对象包含密钥文件中的大部分详细信息。但是,字段
serviceAccountUser
accessToken
refreshToken
clientAuthentication
requestInitializer
具有
null
值。 (我猜这里有问题)

现在,使用

credentialObject
,我继续按照文档编写代码来创建事件。

GoogleCredential credential = doOauth(CREDENTIALS_FILE_PATH);
        Event event = new Event().setSummary("Google I/O 2015").setLocation("800 Howard St., San Francisco, CA 94103")
                .setDescription("A chance to hear more about Google's developer products.");
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

        DateTime startDateTime = new DateTime("2020-12-28T09:00:00-07:00");
        EventDateTime start = new EventDateTime().setDateTime(startDateTime).setTimeZone("America/Los_Angeles");
        event.setStart(start);

        DateTime endDateTime = new DateTime("2020-12-28T17:00:00-07:00");
        EventDateTime end = new EventDateTime().setDateTime(endDateTime).setTimeZone("America/Los_Angeles");
        event.setEnd(end);

        String[] recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=2" };
        event.setRecurrence(Arrays.asList(recurrence));

        EventAttendee[] attendees = new EventAttendee[] { new EventAttendee().setEmail("[email protected]") };
        event.setAttendees(Arrays.asList(attendees));

        EventReminder[] reminderOverrides = new EventReminder[] {
                new EventReminder().setMethod("email").setMinutes(24 * 60),
                new EventReminder().setMethod("popup").setMinutes(10), };
        Event.Reminders reminders = new Event.Reminders().setUseDefault(false)
                .setOverrides(Arrays.asList(reminderOverrides));
        event.setReminders(reminders);
        String calendarId = "primary";
        Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName("testapp").build();
        event = service.events().insert(calendarId, event).execute();
        System.out.printf("Event created: %s\n", event.getHtmlLink());

但是,这导致了错误,

{
  "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."
}

Domain-Wide Delegation
上花费时间后,我明白如果我们必须以其他用户的身份从我们的g套件发送事件,这是需要的,而这对于我的问题来说是不需要的。但是,为了调试,我继续提供
Domain-Wide Delegation
并重新运行程序。同样的错误又出现了。

因此,我从

event
对象中删除了受邀者/与会者并重新运行该应用程序。这次程序运行没有任何错误,但是生成了事件链接,点击后显示,
Could not find the requested event

我在 Google 开发者链接上没有看到任何通过 Java 客户端库使用服务帐户的示例。

您能否让我知道这里出了什么问题以及关于如何从我的项目中创建谷歌日历活动的官方/工作文档添加其他(也非 g 套件)用户添加为与会者,这样我就不会必须获得其他用户的同意才能将事件添加到自己的日历中吗?

谢谢你。

java google-api google-calendar-api google-api-java-client service-accounts
3个回答
2
投票

首先我想说这是新事物,如果您看到很多问题,并且教程没有说明您需要这样做,因为服务帐户过去能够发送邀请,这是谷歌锁定的东西大约一年前就下降了。

这应该可行,但我尚未测试它,因为我不再有权访问 Gsuite 帐户。您需要让 gsuite 管理员为另一个用户设置域范围委派。然后,服务帐户需要模拟该用户,以便它显示为该用户是发送邀请的用户。

关于如何添加它的唯一示例是在 .net 中

.net 示例

var gsuiteUser = "[email protected]";
var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(serviceAccount)
            {
                User = gsuiteUser,
                Scopes = new[] { GmailService.Scope.GmailSend, GmailService.Scope.GmailLabels }

            }.FromCertificate(certificate);

Java示例猜测

我不是 Java 开发人员,但 .net 客户端库和 Java 客户端库的开发方式非常接近。我猜您正在寻找一种名为 setServiceAccountUser

的方法
GoogleCredential credential = new  GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                .setServiceAccountScopes(CalendarScopes.CALENDAR)
                .setServiceAccountPrivateKeyFromP12File(credsPath))
                .setServiceAccountUser(gsuiteUser)
                .build();

0
投票

您应该能够通过在 GoogleCredential 对象中将 deleerated 变量设置为用户的电子邮件来模拟帐户用户。

GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("credentials.json"))
    .createScoped(<SCOPES>)
    .createDelegated("[email protected]");

0
投票

域范围的授权是一种糟糕的做法,对于大多数组织来说根本不可接受。

服务帐户应该能够模拟特定的人,而不是整个该死的域......

© www.soinside.com 2019 - 2024. All rights reserved.