2009-10-07 59 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