通过PHP在默认日历(而不是服务帐户日历)中创建Google日历事件

时间:2018-08-21 08:02:06

标签: php google-calendar-api

要在Google日历中添加活动,我创建了一个Google服务帐户,从中获得了客户ID,服务帐户名称和p12密钥。现在,根据我的代码,它正在创建一个事件,但是它创建了自己的新日历,以使用服务帐户的名称插入该事件。我想在我的Google帐户的默认日历中添加事件。下面是服务帐户事件插入的工作代码。

function calendarize ($title, $desc, $start_datetime, $end_datetime, $gmail_id, $location) 
{
  $start_ev_datetime = new DateTime($start_datetime, new DateTimeZone('America/Chicago'));
  $start_ev_datetime = $start_ev_datetime->format('c');
  $end_ev_datetime = new DateTime($end_datetime, new DateTimeZone('America/Chicago'));
  $end_ev_datetime = $end_ev_datetime->format('c');


  //Google credentials
  $client_id = '**********.apps.googleusercontent.com';
  $service_account_name = '*******.iam.gserviceaccount.com';
  $key_file_location = '**************.p12';
  if (!strlen($service_account_name) || !strlen($key_file_location))
    echo missingServiceAccountDetailsWarning();
  $client = new Google_Client();
  $client->setApplicationName("App Name");

  if (isset($_SESSION['token']) && $_SESSION['token']) {
    $client->setAccessToken($_SESSION['token']);
    if ($client->isAccessTokenExpired()) {
      $access_token= json_decode($_SESSION['token']);
      $client->refreshToken($access_token->refresh_token);
      unset($_SESSION['token']);
      $_SESSION['token'] = $client->getAccessToken();
    }
  }


$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
  $service_account_name,
  array('https://www.googleapis.com/auth/calendar'),
  $key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
  try {
    $client->getAuth()->refreshTokenWithAssertion($cred);
  } catch (Exception $e) {
    var_dump($e->getMessage());
  }
}
$_SESSION['service_token'] = $client->getAccessToken();
$calendarService = new Google_Service_Calendar($client);
$calendarList = $calendarService->calendarList;


//Set the Event data
$event = new Google_Service_Calendar_Event();
$event->setSummary($title);
$event->setDescription($desc);
$event->setLocation($location);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime($start_ev_datetime);
$start->setTimeZone('America/Chicago');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime($end_ev_datetime);
$end->setTimeZone('America/Chicago');
$event->setEnd($end);
try {
 $createdEvent = $calendarService->events->insert('primary', $event);
} catch (Exception $e) {
 var_dump($e->getMessage());
}

$acl = $calendarService->acl->listAcl('primary');
$userExistFlag = false;

foreach ($acl->getItems() as $rule) {
  if($rule->getId() == 'user:'.$gmail_id){
    $userExistFlag = true;
  }
}

if(!$userExistFlag){
  $scope = new Google_Service_Calendar_AclRuleScope();
  $scope->setType('user');
  $scope->setValue( $gmail_id );

  $rule = new Google_Service_Calendar_AclRule();
  $rule->setRole( 'owner' );
  $rule->setScope( $scope );

  $result = $calendarService->acl->insert('primary', $rule);
}

echo 'Event Successfully Added with ID: '.$createdEvent->getId();
}

1 个答案:

答案 0 :(得分:0)

记住一个服务帐户不是您。服务帐户是其自己的虚拟用户。它有自己的Google日历帐户,这就是为什么在其日历而不是您的日历上创建事件的原因。

获取服务帐户的电子邮件地址,并与服务帐户共享您的个人日历,就像您与Web应用程序中的任何其他用户共享日历一样。与服务帐户共享日历后,便可以访问它。注意日历ID,您将可以使用它来更新日历。

背景信息

$createdEvent = $calendarService->events->insert('primary', $event);

primary捐赠当前已认证用户的主要日历。那就是服务帐户的主日历。