为其他用户创建日历事件

Creating Calendar Events for Other Users

我四处寻找,但没有找到任何具体指向这一点的内容。是否可以在其他用户日历中创建日历事件?我们公司有 Google 个应用程序,所以每个人都在我们的域中。我发现的所有内容都指向其他用户需要批准日历事件。

我负责的最终结果是当员工批准休假时,它会发送到员工和主管日历。我确定那里有东西,我只是没有运气在任何地方找到它。

您需要使用服务帐户来模拟用户才能实现此目的。您可以找到有关如何进行此授权的文档 right here。完成域范围的委托后,您可以使用类似这样的方法来完成您的任务:

<?php

session_start();

//INCLUDE PHP CLIENT LIBRARY
require_once 'google-api-php-client-2.0.3/vendor/autoload.php';

putenv('GOOGLE_APPLICATION_CREDENTIALS=yourkey.json'); // yourkey = the name of your json client secret file.

//set the required scopes
$scopes = array("https://www.googleapis.com/auth/calendar");

// Create client object
$client = new Google_Client(); 
$client->useApplicationDefaultCredentials();
$client->addScope($scopes);

$client->setSubject("user@thedomain.com");

$cal = new Google_Service_Calendar($client);    

$event = new Google_Service_Calendar_Event(array(
    'summary' => 'Test Event',
    'location' => 'Some Location',
    'description' => 'Google API Test Event',
    'start' => array(
      'dateTime' => '2017-04-12T05:00:00-06:00'   
    ),
    'end' => array(
      'dateTime' => '2017-04-12T05:25:00-06:00'
    ),  
    'reminders' => array(
      'useDefault' => FALSE,
      'overrides' => array(
        array('method' => 'email', 'minutes' => 24 * 60),
        array('method' => 'popup', 'minutes' => 10)
      ),
    ),
    'attendees' => array(
      array('email' => 'userone@domain.com'),
      array('email' => 'usertwo@domain.com')
    )
));

$calendarId = 'primary';
$event = $cal->events->insert($calendarId, $event, array('sendNotifications' => TRUE));

printf('Event created: %s<br>', $event->htmlLink);    

?>

请注意:以上示例是使用 Google API PHP 客户端库执行的。有关其他示例,请参阅 official documentation。希望这对您有所帮助!