【问题标题】:Google Calendar PHP API can not create calendarGoogle Calendar PHP API 无法创建日历
【发布时间】:2012-12-12 23:09:36
【问题描述】:

我在使用 PHP 客户端库使用 Google 日历 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);

【问题讨论】:

  • 这是使用V3的日历API
  • 我不明白你的意思。你检查过 Apache error.log 吗?
  • 这段代码运行后,新创建的日历不应该显示在谷歌日历网站的“我的日历”下吗?
  • 您是否已将其导入您的 Google 日历帐户?
  • 请注意标签是独立的。也就是说,结合googlecalendarapi 并不意味着您在谈论Google Calendar API。有一个单独的标签。

标签: php google-calendar-api


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多