Google日历API:选择/创建日历?

Ant*_*ony 11 php mysql google-calendar-api

因此,当计划管理员发布/更新计划时,我希望我们的工作日历能够自动与每个员工的Google日历同步.用户最初会选择使用AuthSub令牌,但之后应该是自动的.为了避免与他们安排的其他事件发生冲突,我想创建一个名为"Work App"的新日历或其他相当独特的日历.然后,对于更新,它将在创建新事件之前简单地删除该范围内的任何事件.所以我有一些问题......

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

  2. 我可以创建日历吗?我是否需要让选择创建此类日历的用户?如果是这样,是否有一个我可以生成的URL来创建一个像添加事件一样的URL?(任何使事情简单和一致的事情,对吧?)

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

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

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

Ant*_*ony 26

好吧,既然没有人回答,我决定开始浏览Google Calendar API的非PHP文档,特别是.NET的东西,只是原始协议中的一点.你不知道吗......

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

当然,这个文档在PHP领域中无处可见,并且似乎没有一对一的关联.为了创建新的日历,我首先尝试了一些明显的东西,然后,破产,尝试了一些不那么明显的工作.我想我会分享,以防无线电沉默的原因是没有人知道答案,但肯定会想.

要创建新日历:

这有两个关键:

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

  2. 您必须在方法中设置发布网址,否则将转到默认供稿网址.

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

 //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);
 }
Run Code Online (Sandbox Code Playgroud)

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

将事件添加到非主日历

您可以猜到的一个简单部分是您需要再次设置该可选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>";
Run Code Online (Sandbox Code Playgroud)

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