终于找到了解决办法。在这里发帖,如果我有空闲时间,可能会在以后制作一个完整的教程:)。希望它可以帮助其他人。
https://console.developers.google.com/iam-admin/projects
创建一个项目,它应该在创建后将您带到控制台...如果不是这里是控制台链接https://console.developers.google.com/
在库部分启用“Google Calendar API”。搜索 Google Calendar API,选择它并使用顶部显示的启用按钮。
转到凭据部分并添加服务帐户密钥。新的服务帐号(任意名称)。
卷:
项目>查看器
App Engine > App Engine 查看器
存储 > 存储对象查看器
您可能不需要所有这些,但这似乎是对我有用的组合。确保选择 JSON,然后在创建时下载一个 .json 文件。它只下载一次,无法重新下载,因此请确保下载并保存在安全的地方。
在此处https://developers.google.com/google-apps/calendar/downloads 下载最新的 PHP API 并将其上传到您网站上的任何位置。相当大,包含大量支持文件,因此可能需要一段时间才能上传。
那么这是对我有用的 PHP 代码 :)
CALENDARID 是您日历的 ID...
从您的谷歌日历中,使用弹出窗口并选择“日历设置”。然后在“日历地址:”部分的底部记下日历 ID。如果您通过 gSuite 工具设置,则应该是某种 google .com 电子邮件地址或您的电子邮件地址。
"YOURJSON.json" 是您之前下载的 json 文件的名称。保持安全的建议是将其上传到您网站的根目录上方并从那里链接到它。由于它仅具有读取角色,因此不会引起任何问题,因此更加理智以防万一。
require_once 行是您将 google API 上传到服务器的任何位置。不要忘记 /vendor/autoload.php 部分:)。
这是对我有用的解决方案 (2017-04-24)。 PHP 代码的最后一行是一个通用的 echo 命令,因此您可以看到您获得的数组和所有可用的数据。我唯一没有看到的是事件的自定义颜色。有它的空间,但似乎没有过来。
<?php
$maxEvents = 100;
$minStartDate = date('c');
$calendarId = 'CALENDARID';
//path to the google API on your server
require_once 'inc/google-api-php-client-2.1.3_PHP54/vendor/autoload.php';
//set environment variable to use your downloaded Service account key
putenv("GOOGLE_APPLICATION_CREDENTIALS=YOURJSON.json");
$scope = 'https://www.googleapis.com/auth/calendar.readonly';
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes($scope);
$service = new Google_Service_Calendar($client);
//options link
// https://developers.google.com/google-apps/calendar/v3/reference/events/list
$options = array(
'maxResults' => $maxEvents,
'orderBy' => 'startTime',
'singleEvents' => TRUE,
//UNIX timestamp format
'timeMin' => $minStartDate,
//to use a calendar other than the default uncomment and enter the calendar's ID
//not really needed here since you're using the $calendarId but does pull another calendar and more for completeness
//'iCalUID' => 'CAL_ID_FROM_GOOGLE_CALENDAR'
);
$results = $service->events->listEvents($calendarId, $options);
//echo 'results<br><pre>';print_r($results); echo '</pre><br>';
?>