【发布时间】:2016-03-16 07:59:57
【问题描述】:
我正在尝试使用 PHP 和 Google 的官方 PHP 客户端库来阅读我的 Google 日历活动。
我在Google Developers Console 中创建了一个service account,它似乎可以工作,我可以连接到日历但我得到一个空数组:
Google_Service_Calendar_CalendarList 对象 ( [collection_key:protected] => 项目 [internal_gapi_mappings:protected] => 数组 ( ) [etag] => [itemsType:protected] => Google_Service_Calendar_CalendarListEntry [itemsDataType:protected] => 数组 [种类] => [nextPageToken] => [nextSyncToken] => [modelData:protected] => 数组 ( [error] => 数组 ( [errors] => 数组 ( [0] => 数组 ( [域] => 全局 [原因] => 权限不足 [消息] => 权限不足 ) ) [代码] => 403 [消息] => 权限不足 ) ) [已处理:受保护] => 数组 ( ) )
我的代码:
define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
session_start();
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setAuthConfigFile('client_secret.json');
$client->setAccessType('offline');
$client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
#$client->setUseObjects(true);
$cal=new Google_Service_Calendar($client);
$calendarList=$cal->calendarList->listCalendarList();
while(true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
echo $calendarListEntry;
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/google/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
谁能告诉我我做错了什么 在此先感谢您的帮助。
【问题讨论】:
标签: php google-calendar-api google-api-php-client service-accounts