Google日历API、getItem和getSummery均不返回任何内容


Google Calendar API, getItem and getSummery returns nothing

我似乎遇到了一个谷歌日历api问题。我真的看不出我在哪里犯了错误,所以如果能多看一双眼睛,我将不胜感激。简而言之,问题是应该输出事件的代码部分。。没有(代码的"结果"部分。

require_once "./google-api-php-client/src/Google/Client.php";
require_once "./google-api-php-client/src/Google/Service/Calendar.php";
// Service Account info
$client_id          = "XXXXXX-XXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com";
$app_name               = "Some-name";
$service_account_name   = 'XXXXX-XXXXXXXXXXXXXXXXXXXXXXX@developer.gserviceaccount.com';
$key_file_location  = 'google-api-php-client/Some-name-xxxxxxxxxx.p12';
$cal_id             = "primary";
// Service Account info
$client_id              = $client_id;
$service_account_name   = $service_account_name;
$key_file_location  = $key_file_location;
// Calendar id
$calName                = $cal_id;

$client = new Google_Client();
$client -> setApplicationName($app_name);
$service = new Google_Service_Calendar($client);
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/calendar.readonly'),
    $key
);
$client->setAssertionCredentials($cred);
$cals = $service->calendarList->listCalendarList();
print_r($cals);
echo "<br>events<br>";
$events = $service->events->listEvents($calName);
var_dump($events);
echo "<br>result:<br>";
// the following is what returns nothing, works fine until this point.
$i = 0;
foreach ( $events->getItems() as $event ) {
    echo 'i:'.$i.' '.$event->getSummary();
    $i++;
}
echo 'end';

将$events更改为以下行:

$events=$service->events->listEvents('primary',$params);

在listEvents方法中添加$calName并发送$params,而不是primary。

希望这能奏效。