2009-10-07 62 views
11

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

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

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

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

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

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

+0

我删除了谷歌应用程序引擎标记,因为我不认为您正在使用它。 – 2009-10-07 22:37:23

回答

26

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

如果你去到.NET文档它提到酷功能,具体如何认证用户创建新的非主日历以及如何将事件添加到非主要日历。

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

要创建一个新的日历:

有两个关键是:

  1. 你必须使用同样的方法添加日历事件,这是insertEvent()

  2. 你有在方法中设置发布的网址,否则这些网址会转到默认的Feed网址。

此示例检查,看是否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),棘手的部分是获得日历的网址。与“拥有的日历”路径不同,特定的日历不仅具有用户特定的信息,还具有某种类型的哈希查找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>"; 

感谢谁看了我的问题,做了它的任何想法。如果有人知道一种方法来收紧上面的代码(也许我不需要两个循环?),我很想获得反馈。

+1

感谢您的自我调查和解答。你介意给我们一个你提到的.NET文档的链接吗?再次感谢。 – 2010-02-18 15:22:15

+1

您是否发现如何从非初选中选择特定的日历? – glaz666 2010-05-31 12:47:58

1

我相信Anthony是指Google日历API网站上的v1文档或v2文档。 我通过遵循v2 protocol guide,在Perl中很容易地实现了这一点。

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