Google Calendar API,getItem和getSummery均未返回任何内容

时间:2014-11-10 08:59:44

标签: php api google-calendar-api

我似乎遇到了谷歌日历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';

1 个答案:

答案 0 :(得分:0)

将$ events更改为以下行:

$ events = $ service-&gt; events-&gt; listEvents(&#39; primary&#39;,$ params);

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

希望这有效。