我已经创建了一个服务帐户来访问某个Google日历。此服务帐户用户还获得了日历设置中的许可,如此博客中所述:“ allow my user to add meeting in my calendar with Google Calendar API with PHP without auth”。
正如我在Google Cloud Platform的API +服务下看到的->服务帐户部分中的凭据,每次触发此php脚本时,创建的服务帐户都会与所有服务一起使用(最近30天),但是在浏览器中窗口我没有错误,身份验证会出现问题,但是:未捕获的错误:调用php脚本的null上的成员函数getSummary()在行上...Google Cloud Platform中的脚本和设置如上述文章所述。
任何想法可能是什么问题?自2019年发布此帖子以来,Google Calendar API有什么变化吗?
require '../google-api-php-client/vendor/autoload.php';
ini_set('display_errors', 1);
$client = new Google_Client();
$client->addScope("https://www.googleapis.com/auth/calendar");
$client->setAuthConfig(dirname(__FILE__).'/credentials/credentials.json');
$service = new Google_Service_Calendar($client);
$calendarList = $service->calendarList->listCalendarList();
while(true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
echo $calendarListEntry->getSummary();
echo "\n------------------------------\n\n";
// get events
$events = $service->events->listEvents($calendarListEntry->id);
foreach ($events->getItems() as $event) {
echo "- " . $event->getSummary() . "\n";
echo "- " . $event->getStart()->getDateTime() . "\n\n";
}
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
echo "break";
break;
}
}
echo "calendar summary: " . $calendarListEntry->getSummary();
echo "\ncalendar id: " . $calendarListEntry->getId();
echo "\n------------------------------\n\n";
显然$ calendarList和$ calendarListEntry为空...。
<< function getSummary()之所以不能呈现任何结果(为null或为空)的原因是因为新的服务帐户用户的日历列表为空。通过在任何其他日历中授予服务帐户用户权限,不会自动将此日历插入服务帐户用户的calendarList。因此,您首先必须为服务帐户插入/创建一个calenderList:
$calendarListEntry = new Google_Service_Calendar_CalendarListEntry();
$calendarListEntry->setId("calendar_address_you_want_to_add");
$createdCalendarListEntry = $service->calendarList->insert($calendarListEntry);
echo $createdCalendarListEntry->getSummary();
[现在使用第一个脚本时,您将获得getSummary()函数和getId()函数的calendarEvents结果(就日历中发布的任何即将发生的事件而言。)>