Google API PHPクライアント - カレンダーリストとイベントを取得する
-
20-12-2019 - |
質問
Google Calendar APIのドキュメントは最新のもので、簡単なカレンダーリストでも取得できません。これが私のコードです。
<?php
require_once "includes/partials/_header.php";
set_include_path(get_include_path() . '/google-api-php-client/src');
$client_id = 'xxxx.apps.googleusercontent.com';
$service_account = 'xxxx@developer.gserviceaccount.com';
$p12 = 'xxxx-privatekey.p12';
session_start();
require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';
$client = new Google_Client();
$client->setApplicationName("Calendrier");
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
$key = file_get_contents($p12);
$client->setClientId($client_id);
$cred = new Google_Auth_AssertionCredentials(
$service_account,
array('https://www.googleapis.com/auth/calendar'),
$key);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$cal = new Google_Service_Calendar($client);
$events = $cal->calendarList->listCalendarList();
echo "<pre>";
print_r($events);
echo"</pre>";
while(true) {
foreach ($events->getItems() as $event) {
echo $event->getSummary();
print_r($event);
}
$pageToken = $events->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$events = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
?>
.
ドキュメントは時代遅れのものだったので、私はさまざまなサイトからこのコードをたくさん持っていました、今、私は一日中googlingしているので全く立ち往生しています。洞察?
私がこれまでに得られるのは、「アイテム」
でこのような回答です。Google_Service_Calendar_CalendarList Object
(
[etag] => "xxxxxxxxxxxx"
[itemsType:protected] => Google_Service_Calendar_CalendarListEntry
[itemsDataType:protected] => array
[kind] => calendar#calendarList
[nextPageToken] =>
[nextSyncToken] => 00001401741798955000
[collection_key:protected] => items
[modelData:protected] => Array
(
[items] => Array
(
)
)
[processed:protected] => Array
(
)
)
. 解決
allright、これは私がこれに費やしたときに恥ずかしいことです。何らかの理由でカレンダーを公開することは決してなかった。私はあなたがプロジェクトアカウントからカレンダーにアクセスできるすべての許可手順で想定していました。推測しないでください。
Easy Fix ...その設定でカレンダーを公開するだけです。
イベントの場合$ CAL-> CalendarList部分を
に置き換えます$cal->events->listEvents('calendarID', $optionalParams);
.
注意:セッション内の許可/アクセストークンのすべてがすべて必要とされているのは、Google CalのDocsページが古いPHPクライアントを使用しているようです。私はそれがサービスアカウントのみ(SetAssertionCredentials)のみを使用して、セッションIMを確信していない状態で機能するはずです。
所属していません StackOverflow