This post is under 軟體開發
為了在旅聯網增加行事曆的功能,研究如何納入Google Calender API。找尋一些網路上的中文範例都指向使用Zend Framework就可以順利完成Calendar讀取新增修改刪除等基本動作。譬如照著這位作者的Google Calendar API系列文章,當可以開發出整合Google Calendar API的應用。
實際嘗試的結果發現幾個關卡:
- Google Calendar API已經升級為3.0,而此範例使用的仍是2.0的版本。得先到Google 帳號取啟用用Google Calendar API 3的服務。
- 在local本機上按照範例可以實作出來。但移植到遠端伺服器之後卻出現授權的問題。因為Google Calendar API 3已經使用OAuth的使用者認證方式,而舊版的方式是輸入使用者的帳號密碼。
懶得研究google calendar 3的API,仍想要使用現在的Zend範例。來到Zend Famework網站的使用者認證說明文件,看到可以改使用OAuth 的session token方式登入:2014/11/18之後不能再使用 zend 呼叫 google calendar API。改用這一篇做法:http://stackoverflow.com/questions/27011264/authentification-zend-gdata-403-forbidden/27034888 (150821 更新)
$client = Zend_Gdata_AuthSub::getHttpClient($accessToken);
- 但如何取得accessToken?在這篇國外的文章中敘述了使用OAuth的Google Calender API 範例: How to embed a Google Calendar into a web page and use it to accept online bookings/appointments from other online users。
- 在step 4要注意的事項很多,包括 Part 2: OAuth2 and Configuring Your ‘Application’ With Google。此處取得Client ID的介面稍微有點變動。而且必須先為這個project設定名稱,否則未來取得授權時會出現錯誤。
- google透過code取得token的方式跟facebook類似。但以code取得的是fresh_token(永不失效),需要再從fresh_token再取得asscessToken。
- 有時在step5取得fresh_code時,會有問題。可能跟scope有關。可參考step4的連結文章中的留言多試幾次。
旅聯網的行事曆似乎不能用
http://www.waytogo.cc/calendar.php
張貼行程之後沒反應
google 的API認證取得方式有所調整,答案似乎在這一篇:
http://stackoverflow.com/questions/24946453/google-api-oauth2-service-account-error-invalid-grant 。但還沒時間研究,如果有人先研究出來,要告訴我喔。
正確解答在這一篇:http://stackoverflow.com/questions/27011264/authentification-zend-gdata-403-forbidden/27034888#27034888。主要原因是 2014/11/18 之後不能再使用 zend 去呼叫 google calendar api;另外就是google accessToken太久沒用也失效了。
重點是:行事曆又可以正常使用了。