我的 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);
无论如何要插入到另一个用户的日历列表中?
答案 0 :(得分:2)
服务帐户有自己的谷歌日历帐户,您执行的任何操作都将在服务帐户谷歌日历帐户上执行。
但是,如果您想在声明您的客户时冒充用户,您需要确保添加了主题或您想冒充的人。
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);