0

我有一个关于Google Calendar API的问题。使用服务帐户在PHP上使用Google Calendar API

我使用PHP获取Google Calendar API并使用服务帐户创建新事件。

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

问题的原因是什么?

它是一个规范,PHP无法读取从一个服务帐户从PHP创建的时间表?

设置信息:

  1. 我使用谷歌autoload.php-API PHP客户端-1-高手。

  2. 这是Google日历V3。

  3. 在日历ID中,共享电子邮件帐户(服务帐户)被赋予“日程安排更改权限”并且可以写入日历。

  4. 所有者在Google日历上直接创建的日程安排可以通过php progrum读取,但由服务帐户通过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 = '[email protected]'; 
$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 = '[email protected]'; 
$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); 

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

你能告诉我这个答案吗?

回答

0

你需要记住,一个服务帐户不是你。将服务帐户视为虚拟用户。它有自己的Google日历帐户,自己的驱动器帐户,可能还有更多。将服务帐户中的日历插入到服务帐户所拥有的日历中。

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

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

如果你想要一个特定的用户添加事件,那么你应该使用Oauth2。

+0

谢谢@DaImTo你说的没错。 – Andy

相关问题