为其他用户创建日历事件

时间:2018-06-22 19:12:40

标签: google-api google-calendar-api

我已经四处搜寻,但没有发现任何指向这一点的具体信息。是否可以在其他用户日历中创建日历事件?我们公司拥有Google Apps,因此每个人都在我们的域中。我找到的所有内容均指向批准日历事件所需的其他用户。

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

1 个答案:

答案 0 :(得分:1)

您将需要使用服务帐户来模拟用户以实现此目的。您可以找到有关如何进行此授权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。希望这会有所帮助!

相关问题