11

所以我希望我们的工作日历在调度经理发布/更新日程时自动与每个员工的谷歌日历同步。用户最初会选择使用 AuthSub 令牌,但之后它应该是自动的。为了避免与他们安排的其他活动发生冲突,我想创建一个名为“工作应用程序”或其他相当独特的新日历。然后对于更新,它会在创建新事件之前简单地删除该范围内的所有事件。所以我有一些问题...

  1. 如何使用 API 选择我想要的特定日历?(顺便说一句,这一切都在 PHP 中,使用 Zend 框架)。我看到可以在哪里查询日历列表,但文档没有显示如何添加到该特定日历。

  2. 我可以创建日历吗?我是否需要让选择加入的用户创建这样的日历?如果是这样,我是否可以为他们生成一个 URL 来创建一个类似于添加事件的 URL?(任何让事情变得简单和一致的方法,对吧?)

  3. 显然,我不希望用户存储他们的凭据,但我也不想为每个更新请求访问权限。我是否需要 OAuth 令牌才能获得持久访问权限?我知道 URL 令牌是一次性使用的,但我可以存储会话令牌并在一周后重新使用它吗?

  4. 除了查询每个事件以避免重复之外,还有其他方法吗?我昨晚正在测试这个,现在我有 7 个相同 50 个事件的实例。我不想在每次添加之前通过检查来添加服务器时间。

  5. 最后,有没有办法添加几个事件,甚至只是将 ics 文件推送到日历。现在,我让脚本执行 SQL 查询,并在循环结果时添加每个事件。这似乎比预期花费的时间要长得多,因此仅构建 ics 文件或将所有事件添加到一个对象并一次添加它们会更好。谢谢!

4

2 回答 2

26

好吧,既然没有人回答,我决定开始研究 Google 日历 API 的非 PHP 文档,特别是 .NET 的东西和原始协议的一些内容。而且你不知道吗...

如果您访问 .NET 文档,它会提到很酷的功能,特别是如何为经过身份验证的用户创建新的非主日历以及如何将事件添加到非主日历。

当然,该文档在 PHP 领域中没有出现,并且似乎没有一对一的关联。对于新日历的创建,我首先尝试了一些明显的东西,然后,为了破产,尝试了一些不太明显但有效的东西。我想我会分享一下,以防无线电沉默的原因是没有人知道答案,但肯定想知道。

要创建新日历:

这有两个关键:

  1. 您必须使用相同的方法来添加日历事件,即insertEvent()

  2. 您必须在方法中设置发布 URL,否则将转到默认提要 URL。

此示例检查 App Calendar 是否已存在,如果不存在,则创建它:

 //Standard creation of the HTTP client
 $gdataCal = new Zend_Gdata_Calendar($client);

 //Get list of existing calendars
 $calFeed = $gdataCal->getCalendarListFeed();

 //Set this to true by default, only gets set to false if calendar is found
 $noAppCal = true;

 //Loop through calendars and check name which is ->title->text
 foreach ($calFeed as $calendar) {
  if($calendar -> title -> text == "App Calendar") {
   $noAppCal = false;
  }
 }

 //If name not found, create the calendar
 if($noAppCal) {

  // I actually had to guess this method based on Google API's "magic" factory
  $appCal = $gdataCal -> newListEntry();
  // I only set the title, other options like color are available.
  $appCal -> title = $gdataCal-> newTitle("App Calendar"); 

  //This is the right URL to post to for new calendars...
  //Notice that the user's info is nowhere in there
  $own_cal = "http://www.google.com/calendar/feeds/default/owncalendars/full";

  //And here's the payoff. 
  //Use the insertEvent method, set the second optional var to the right URL
  $gdataCal->insertEvent($appCal, $own_cal);
 }

你有它。下一个目标是将事件插入该日历,而不是默认日历。

将事件添加到非主日历

您可能猜到的简单部分是您需要再次设置该可选 URL,例如 : insertEvent($newEvent, $calURL),棘手的部分是获取日历的 URL。与“拥有的日历”路径不同,特定的日历不仅包含特定于用户的信息,而且还包含某种哈希查找 ID。

这是代码:

 //Set up  that loop again to find the new calendar:
 $calFeed = $gdataCal->getCalendarListFeed();
 foreach ($calFeed as $calendar) {
  if($calendar->title->text == "App Calendar")
   //This is the money, you need to use '->content-src'
   //Anything else and you have to manipulate it to get it right. 
   $appCalUrl = $calendar->content->src;
 }

 //.......... Some Assumed MySQL query and results .............

      while ($event = $result->fetch_assoc()) {
   $title = $event['description'];

   //Quick heads up
   //This is a handy way of getting the date/time in one expression.
   $eventStart = date('Y-m-d\TH:i:sP', strtotime($event['start']));
   $eventEnd = date('Y-m-d\TH:i:sP', strtotime($event['end']));

   $newEvent = $gdataCal->newEventEntry();
   $newEvent->title = $gdataCal->newTitle($title);
   $when = $gdataCal->newWhen();
   $when->startTime = $eventStart;
   $when->endTime = $eventEnd;

   $newEvent->when = array($when);

   //And at last, the insert is set to the calendar URL found above
   $createdEvent = $gdataCal->insertEvent($newEvent, $appCalUrl);
  }
  echo "<p>".$result->num_rows." added to your Google calendar.</p>";

感谢任何阅读我的问题并对此进行任何思考的人。如果有人知道收紧上述代码的方法(也许我不需要两个循环?)我很想得到反馈。

于 2009-10-10T17:32:28.033 回答
1

我相信 Anthony 指的是 Google Calendar API 网站上的 v1 文档或 v2 文档。通过遵循v2 协议指南,我在 Perl 中很容易地做到了这一点。

关键是更改您要发布到的 URL。代替默认的“/calendar/feeds/default/private/full”,首先获取可用日历的列表,然后从您想要的日历中获取 content/@src 值,然后发布到该日历,例如 /calendar/feeds/ 1234567890abcdefeg%40group.calendar.google.com/private/full

于 2010-06-28T10:17:24.097 回答