问题描述
我正在尝试使用PHP和Google的官方PHP客户端库读取Google日历事件.
I'm trying to read my Google Calendar events using PHP and Googles official PHP client library.
我在 Google Developers Console 中创建了service account
要工作,我可以连接到日历,但是会得到一个空数组:
I created a service account
in the Google Developers Console and it seemed to work, I can connect to the calendar but I get an empty array:
我的代码:
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));
}
有人可以告诉我我做错了什么吗在此先感谢您的帮助.
Can anybody tell me what I do wrongThanks in advance for your help.
推荐答案
您正在使用服务帐户进行连接.服务帐户拥有自己的Google日历帐户,该帐户默认情况下没有任何日历,并且默认情况下无权访问您的Google日历帐户.
You are connecting with a Service account. A service account has its own Google Calendar account which by default does not have any Calendars, and does not by default have access to your Google calendar account.
以下是告诉您的信息:
如果您想授予服务帐户访问Google日历的权限,则需要获取服务帐户的电子邮件地址并与其共享Google日历,就像您要与其他任何用户共享该电子邮件地址一样.
If you want to grant the service account access to your Google Calendar you will need to take the service account email address and share your Google calendar with it like you would share it with any other user.
这篇关于连接Google Calendar API使用php获取空数组结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!