Google Calendar PHP API无法创建日历

时间:2012-12-12 23:09:36

标签: php google-calendar-api

我在使用PHP客户端库时使用Google Calendar API遇到了一些问题。

以下是我的代码:

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
define("CLIENT_ID", "blah blah blah");
define("SERVICE_ACCOUNT_NAME", "blah blahblah");   
define("KEY_FILE", "keykeykey.p12");

$client = new Google_Client();
$client->setApplicationName("Google Calendar Sample");
$client->setUseObjects(true);

session_start();
if (isset($_SESSION['token'])) 
{
   $client->setAccessToken($_SESSION['token']);
}

$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
                                 SERVICE_ACCOUNT_NAME,
                                 array('https://www.googleapis.com/auth/calendar'),
                                 $key)
);

$client->setClientId(CLIENT_ID);
$service = new Google_CalendarService($client);
///////////////////////////////////////////////////////////////////
try
{
    $calendar = new Google_Calendar();
    $calendar->setSummary('test');

    $createdCalendar = $service->calendars->insert($calendar);
    var_dump($createdCalendar);
}
catch(Google_ServiceException $e)
{
    var_dump($e);
}

///////////////////////////////////////////////////////////////////
if ($client->getAccessToken()) 
{
    $_SESSION['token'] = $client->getAccessToken();
}
?>

$ createdCalendar的转储如下:

object(Google_Calendar)[15]
public 'kind' => string 'calendar#calendar' (length=17)
public 'description' => null
public 'summary' => string 'test' (length=3)
public 'etag' => string '"wT71Iy7ZAyY0SAhLu4jxO8w9qqM/zHZjSmLgdGM-FmXSbf2H0YONYb0"' (length=57)
public 'location' => null
public 'timeZone' => null
public 'id' => string '8ejg8n6j1j9utuuh6954jm18lk@group.calendar.google.com' (length=52)

这表明日历是成功创建的,因为谷歌提供了ID。但是,当我在网络浏览器中打开谷歌日历时,此日历不会显示在日历列表中。

有什么想法吗?

顺便说一句,我知道我的2 Legged OAuth2的设置是有效的,因为如果用以下内容替换//////////////之间的代码块,它可以正常工作

$event = new Google_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_EventDateTime();
$start->setDateTime('2012-12-15T16:00:00.000-08:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2012-12-15T19:00:00.000-08:00');
$event->setEnd($end);
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail('testEmail@gmail.com');

$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);

1 个答案:

答案 0 :(得分:1)

我发现我的类似代码为google api用户(NNNNNNNNNNNN@developer.gserviceaccount.com)创建了日历,但不是我。查看第一个现有日历的“ID”,您会看到NNNNNNNNNNNN@developer.gserviceaccount.com