簡體   English   中英

使用服務帳戶在php上使用Google Calendar API

[英]Google Calendar API on php using service account

我對Google Calendar API有疑問。

我使用PHP獲取了Google Calendar API,並使用服務帳戶創建了新事件。

如果我再次在php progrum上閱讀了日歷,則只能加載帶有服務帳戶的php編寫的日程表。

是什么原因引起的?

php無法使用服務帳戶從php創建的日程表中讀取php的規范嗎?

設置信息:

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

  2. 這是Google日歷V3。

  3. 在日歷ID中,共享電子郵件帳戶(服務帳戶)被授予“計划更改權限”,並且可以寫入日歷中。

  4. 所有者可以在php progrum上讀取所有者在Google日歷上直接創建的日程表,但是無法加載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插入服務帳戶的日歷時,即插入服務帳戶擁有的日歷。

但是,事件的創建者是通過服務帳戶的地址而非所有者的郵件帳戶創建的。

事件的創建者是服務帳戶,而服務帳戶應是其創建事件的電子郵件帳戶。

如果要讓特定用戶添加事件,則應使用Oauth2。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM