1

我在使用 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)

这表明日历已成功创建,因为 google 提供了 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);
4

1 回答 1

1

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

于 2013-04-19T18:07:22.097 回答