Google 日历 api CalendarList 列表 return 空列表
Google calendar api CalendarList List return empty list
已下载脚本 here 和设置。但是当我试图获取日历时,它 returns 是一个空列表。我想获取所有日历列表
与帐户共享。请注意,有 4 个日历与我的 Gmail 帐户共享。我也关注这个 instruction.
**Code**
$key = file_get_contents($key_file_location);
$scopes ="https://www.googleapis.com/auth/calendar.readonly";
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array($scopes),
$key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$service = new Google_Service_Calendar($client);
echo "<br>=======================<br>";
var_dump($service->calendarList->listCalendarList());
echo "<br>=======================<br>";
?>
输出
=======================
object(Google_Service_Calendar_CalendarList)[23] protected 'collection_key' => string 'items' (length=5) protected 'internal_gapi_mappings' =>
array (size=0)
empty public 'etag' => string '"1462447526879000"' (length=18) protected 'itemsType' => string 'Google_Service_Calendar_CalendarListEntry' (length=41) protected 'itemsDataType' => string 'array' (length=5) public 'kind' => string 'calendar#calendarList' (length=21) public 'nextPageToken' => null public 'nextSyncToken' => string 'CJi-3srpwswCEjVhYmNkZWZAc2tpbGxmdWwtY29zaW5lLTEyNDYwNi5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbQ==' (length=88) protected 'modelData' =>
array (size=1)
'items' =>
array (size=0)
empty protected 'processed' =>
array (size=0)
empty
=======================
当文件为 json 时,读取键值有一些变化,下面的表格是正确的代码
$key = file_get_contents($key_file_location);
$data = json_decode($key);
$scopes = array("https://www.googleapis.com/auth/calendar.readonly","https://www.googleapis.com/auth/calendar");
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
$scopes,
$data->private_key
);
$client->setAssertionCredentials($cred);
try {
$client->getAuth()->refreshTokenWithAssertion($cred);
} catch (Exception $e) {
$e->getMessage();
}
$calendarList = $service->calendarList->listCalendarList();
var_dump($calendarList);
已下载脚本 here 和设置。但是当我试图获取日历时,它 returns 是一个空列表。我想获取所有日历列表 与帐户共享。请注意,有 4 个日历与我的 Gmail 帐户共享。我也关注这个 instruction.
**Code**
$key = file_get_contents($key_file_location);
$scopes ="https://www.googleapis.com/auth/calendar.readonly";
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array($scopes),
$key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$service = new Google_Service_Calendar($client);
echo "<br>=======================<br>";
var_dump($service->calendarList->listCalendarList());
echo "<br>=======================<br>";
?>
输出
=======================
object(Google_Service_Calendar_CalendarList)[23] protected 'collection_key' => string 'items' (length=5) protected 'internal_gapi_mappings' =>
array (size=0)
empty public 'etag' => string '"1462447526879000"' (length=18) protected 'itemsType' => string 'Google_Service_Calendar_CalendarListEntry' (length=41) protected 'itemsDataType' => string 'array' (length=5) public 'kind' => string 'calendar#calendarList' (length=21) public 'nextPageToken' => null public 'nextSyncToken' => string 'CJi-3srpwswCEjVhYmNkZWZAc2tpbGxmdWwtY29zaW5lLTEyNDYwNi5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbQ==' (length=88) protected 'modelData' =>
array (size=1)
'items' =>
array (size=0)
empty protected 'processed' =>
array (size=0)
empty
=======================
当文件为 json 时,读取键值有一些变化,下面的表格是正确的代码
$key = file_get_contents($key_file_location);
$data = json_decode($key);
$scopes = array("https://www.googleapis.com/auth/calendar.readonly","https://www.googleapis.com/auth/calendar");
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
$scopes,
$data->private_key
);
$client->setAssertionCredentials($cred);
try {
$client->getAuth()->refreshTokenWithAssertion($cred);
} catch (Exception $e) {
$e->getMessage();
}
$calendarList = $service->calendarList->listCalendarList();
var_dump($calendarList);