首页 > 解决方案 > Google API 为用户订阅日历

问题描述

我的 PHP 代码使用服务帐号在 G Suite/Google Workspace 中创建新用户。这部分工作正常,但我想自动为新用户订阅我们的一些标准办公日历。当我运行以下代码时,日历将添加到服务帐户的列表中,而不是新用户。

$service = new Google_Service_Calendar($client);
$calendarListEntry = new Google_Service_Calendar_CalendarListEntry();
$calendarListEntry->setId("CalendarID");
$calendarListEntry = $service->calendarList->insert($calendarListEntry);

无论如何要插入另一个用户日历列表吗?

标签: phpgoogle-calendar-apigoogle-api-php-clientgoogle-workspaceservice-accounts

解决方案


服务帐户有自己的 google 日历帐户,您执行的任何操作都将在服务帐户 google 日历帐户上执行。

但是,如果您想在声明客户时模拟用户,则需要确保添加主题或您希望模拟的人。

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
Set the scopes required for the API you are going to call
$client->addScope(Google\Service\CALENDAR::CALENDAR);
$client->setSubject($user_to_impersonate);

推荐阅读