[分享] Google Calendar API 授權方式介紹
之前有幾位大大已將Google Calendar API的使用方式介紹
本人之前也有對這個部份研讀一下api的部份,希望可以將所有前輩的資料做個整理
將自己研究的部份做個整合。
本篇將介紹Google Calendar api的授權方式,有興趣的朋友歡迎一起討論
-------------本文開始-------------------------------
1.帳號密碼直接輸入
此種方式雖然直覺但是對於要公開給大眾使用,似乎缺少了一點可信度,在保密程
度上也可能因為程式的設計不良導致帳號密碼被擷取,危險性高了些,但是如果因為機制
上需要共用同一組google account似乎是個不錯的選擇!
2.由google登入後產生token
登入帳號密碼驗證由google 登入頁面處理,而驗證後會產生一組token,由這一組
token使api產生gSession使驗證通過。
看到這邊也許不太清楚,以下由程式碼直接介紹:
1. 由上篇文章將library載入
2. 以下兩個php檔案
login.inc.php
<?php
//這邊是在設定程式把Zend Gdata Library 載入程式碼中
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
?>
demo.php
<?php
require_once('config.inc.php');
$googleAccount = 'user@gmail.com'; //Google 帳號
$googlePassword = 'password'; //Google 密碼
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // 提供Calendar 的服務名稱
$client = Zend_Gdata_ClientLogin::getHttpClient($googleAccount,
$googlePassword, $service);
$gdataCal = new Zend_Gdata_Calendar($client);
$calFeed = $gdataCal->getCalendarListFeed();
//echo "<h1>" . $calFeed->title->text . "</h1>\n";
foreach ($calFeed as $calendar) {
echo "<h3>" . $calendar->title->text . "</h3>";
}
?>
demo.php檔案中的$googleAccount、$googlePassword即是Google帳號、密碼,而此頁面
便是使用直接登入的方式。
接下來將此網頁修改成token方式登入,以下幾個檔案請依照著增加
tokenAuth.php
<?php
//取得授權網址
function getGoogleURL($url){
$googleTarget = 'https://www.google.com/accounts/AuthSubRequest';
$scope = 'http://www.google.com/calendar/feeds/&secure=&session=1';
return $googleTarget . '?next=' . $url . '&scope=' . $scope;
}
?>
$url 即授權後轉址位址
$googleTarget 為google授權登入頁面
$scope 因為使用calendar服務,所以scope路徑需設定,如果是採用ssl,secure則為1
displayCal.php
<?php
function displayCalendarList($client){
$gdataCal = new Zend_Gdata_Calendar($client);
$calFeed = $gdataCal->getCalendarListFeed();
foreach ($calFeed as $calendar) {
echo "<h3>" . $calendar->title->text . "</h3>";
}
}
?>
此頁面為顯示最後結果!
最後再來一個頁面將所有的方法結合
tokenDemo.php
<?php
require_once('config.inc.php');
require('tokenAuth.php');
require('displayCal.php');
$client = new Zend_Gdata_HttpClient();
if(isset($_GET['token']) && $_GET['token'] != ''){
$gSession = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token'],
$client);
$client->setAuthSubToken($gSession);
displayCalendarList($client);
}else{
$googleURL = getGoogleURL('http://localhost/CalDemo/demo.php');
echo '<a href="' . $googleURL . '"> Please login to your Google
Account</a>';
}
?>
當google驗證完之後會回傳一組token,所以先判斷有沒有這組token存在,如果有就轉向
google 驗證頁面,存在的話就將這組token轉換成session後再執行授權,整個授權的流
程如下圖。
點下連結後立即轉向google驗證頁面
登入自己的google帳號密碼
會出現允許存取的服務及需求網址,選擇『授予存取權』,立即會轉向剛剛設定的網址
google會授予一組token,而此token經過api轉換後變為一組session,使用此session轉
換為calendar服務。
注意:
如果有嘗試的話可以發現token只能使用一次,因此如果需要多個頁面使用google服務就
需要將轉換後的gsession儲存起來(利用cookie or session),但是經過本人測試如果
gSession使用頻率太過頻繁的話將發生gSession失效的狀況,在實做的時機可考慮使用
try catch,避免一堆page die的狀況。
(如果有人知道為何會發生錯誤,歡迎一起來討論。)
後記:
這邊文章希望可以留給有需要的人。
不過這幾天我又要收假了,七月份才會出新稿,我也不想富奸啊><",真的很希望有興趣
的同好一起來討論。
--------------------本文結束---------------
圖文版:
http://clonn.blogspot.com/2010/06/google-calendar-api-in-php_13.html
--
猜透女生的心意,比處理別人的bug還難
To guess girls' thought is more difficult than debug.
<< http://clonn.blogspot.com/ >>
噗 http://www.plurk.com/clonn 浪
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 124.9.72.32
推
06/14 01:49, , 1F
06/14 01:49, 1F
→
06/14 11:11, , 2F
06/14 11:11, 2F
PHP 近期熱門文章
PTT數位生活區 即時熱門文章