首页 文章

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

提问于
浏览
11

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

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

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

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

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

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

2 回答

  • 1

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

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

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

    要创建新日历:

    这有两个关键:

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

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

    此示例检查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);
     }
    

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

    将事件添加到非主日历

    您可能猜到的一个简单部分是您需要再次设置该可选URL,例如: insertEvent($newEvent, $calURL) ,棘手的部分也是在那里获取日历's URL. Unlike the 256226 path, specific calendars not only have user-specifc info in it, they have some kind of hash-lookin' 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>";
    

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

  • 26

    我相信Anthony指的是Google Calendar API网站上的v1文档或v2文档 . 通过跟随v2 protocol guide,我很容易在Perl中管理它 .

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

相关问题