简体   繁体   English

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

[英]Google calendar event creation via PHP in the default calendar, not service account calendar

To add an event in Google calendar, I created a Google service account from which I got Client Id, Service Account Name and p12 key. 为了在Google日历中添加活动,我创建了一个Google服务帐户,从中获得了客户ID,服务帐户名称和p12密钥。 Now from my code it is creating an event but it created its own new calendar to insert the event with the name of service account. 现在,根据我的代码,它正在创建一个事件,但是它创建了自己的新日历,以使用服务帐户的名称插入该事件。 I want to add event in the default calendar of my Google account. 我想在我的Google帐户的默认日历中添加事件。 Below is the working code for service account event insert. 以下是服务帐户事件插入的工作代码。

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();
}

Remember a service account is not you. 请记住,服务帐户不是您。 A service account is its own dummy user. 服务帐户是其自己的虚拟用户。 It has its own google calendar account this is why its creating events on Its calendar and not yours. 它有自己的Google日历帐户,这就是为什么在其日历而不是您的日历上创建事件的原因。

Take the service account email address and share your personal calendar with the service account like you would share it with any other user in the web application. 获取服务帐户的电子邮件地址,并与服务帐户共享您的个人日历,就像您与Web应用程序中的任何其他用户共享它一样。 Once you have shared the calendar with the service account it will have access to it. 与服务帐户共享日历后,便可以访问它。 Note the calendar Id and you will be able to use that to update your calendar. 注意日历ID,您将可以使用它来更新日历。

Background Info 背景资料

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

primary donates the primary calendarof the currently authenticated user. primary捐赠当前已认证用户的主要日历。 That being the service accounts primary calendar. 那就是服务帐户的主日历。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM