问题
I'm trying to read my Google Calendar events using PHP and Googles official PHP client library.
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:
Google_Service_Calendar_CalendarList Object ( [collection_key:protected] => items [internal_gapi_mappings:protected] => Array ( ) [etag] => [itemsType:protected] => Google_Service_Calendar_CalendarListEntry [itemsDataType:protected] => array [kind] => [nextPageToken] => [nextSyncToken] => [modelData:protected] => Array ( [error] => Array ( [errors] => Array ( [0] => Array ( [domain] => global [reason] => insufficientPermissions [message] => Insufficient Permission ) ) [code] => 403 [message] => Insufficient Permission ) ) [processed:protected] => Array ( ) )
my code:
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 wrong Thanks in advance for your help.
回答1:
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.
That is what the following message is telling you:
Insufficient Permission
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.
来源:https://stackoverflow.com/questions/36029805/connect-google-calendar-api-get-empty-array-result-with-php