Google API 日历 php 使用服务帐户

Google Calendar API on php using service account

我有一个关于 Google Calendar API 的问题。

我使用 PHP 获取了 Google 日历 API 并使用服务帐户创建了新活动。

如果我再次阅读 php progrum 上的日历,只能加载从 php 使用服务帐户编写的日程表。

问题的原因是什么?

是否是 php 无法在使用服务帐户从 php 创建的计划中读取的规范?

设置信息:

  1. 我正在使用 google-api-php-client-1-master.autoload.php.

  2. 是Google日历V3.

  3. 在日历 ID 中,共享电子邮件帐户(服务帐户)被赋予 "schedule change authority" 并且可以写入日历。

  4. 所有者直接在Google日历上创建的日程可以被php程序读取,但是php使用服务帐户创建的日程可以不是负载。

我的代码

//read event

$json_path = '../json/XXXX-123465789.json';
$json_string = file_get_contents($json_path, true);
$json = json_decode($json_string, true);
$private_key = $json['private_key'];
$client_email = $json['client_email'];
$scopes = array(Google_Service_Calendar::CALENDAR);
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key
);

$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP API");
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Calendar($client);


$read_start = mktime(0, 0, 0, 4, 10, 2017);
$read_end = mktime(0, 0, 0, 4, 17, 2017);

$calendarId = '123456789@group.calendar.google.com';
$optParams = array(
    'maxResults' => 99,
    'orderBy' => 'startTime',
    'singleEvents' => TRUE,
    'timeMin' => date('c', $read_start_date),
    'timeMax' => date('c', $read_end_date),
);

$results = $service->events->listEvents($calendarId, $optParams);



//create event

$json_path = './json/xxx-123456789.json';
$json_string = file_get_contents($json_path, true);
$json = json_decode($json_string, true);

$calendarId = '123456789@group.calendar.google.com';
$private_key = $json['private_key'];
$client_email = $json['client_email'];
$scopes = array(Google_Service_Calendar::CALENDAR);
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key
);

$client = new Google_Client();
$client->setApplicationName("Google Calendar");
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Calendar($client);


$meeting_end_time = _get_sum_time($set_time,$meeting_time);
$event = new Google_Service_Calendar_Event(array(
    'summary' => 'created by service account',
    'start' => array(
        'dateTime' => '2017-04-04T00:12:00+09:00',
        'timeZone' => 'Asia/Tokyo',
    ),
    'end' => array(
        'dateTime' => '2017-04-04T00:13:00+09:00',
        'timeZone' => 'Asia/Tokyo',
    ),
));
$event = $service->events->insert($calendarId, $event);

但是,事件的创建者是通过服务帐户的地址创建的,而不是所有者的邮件帐户。

你能告诉我这个答案吗?

您需要记住服务帐户不是您。将服务帐户视为虚拟用户。它有自己的 Google 日历帐户、自己的驱动器帐户,可能还有更多。当您将 a 插入到服务帐户上的日历中时,您将插入到服务帐户拥有的日历中。

However, the creator of the event is created by address of the service account, not the owner's mail account.

活动的创建者是服务帐户,应该是创建活动的电子邮件帐户。

如果您希望特定用户添加事件,那么您应该使用 Oauth2。