1
我在使用Google Calendar API使用PHP客戶端庫時遇到了一些問題。Google日曆PHP 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 '[email protected]' (length=52)
這表明曆法創建成功,因爲ID是由谷歌提供。但是,當我在我的Web瀏覽器中打開Google日曆時,此日曆不會顯示在日曆列表中。
任何想法?
順便說一句,我知道我的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('[email protected]');
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);
這是使用V3的日曆API – user1772255
我不明白你的意思。你檢查過Apache的error.log嗎? – feeela
這段代碼運行後,不應該在Google日曆網站的「我的日曆」下顯示創建的新日曆嗎? – user1772255