作者clonn (clonn)
看板NUU_IM
标题[情报] Google Calendar API in PHP 教学(三) - 帐号验证
时间Mon Jun 14 00:09:59 2010
图文版:
http://clonn.blogspot.com/2010/06/google-calendar-api-in-php_13.html
=====================本文开始============================
上篇文章中已教导如何使用api,而Google对於帐号验证方式有以下两种:
(如果对於验证没有什麽兴趣的朋友可直接跳过)
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 = '
[email protected]'; //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的状况。
(如果有人知道为何会发生错误,欢迎一起来讨论。)
後记:
这边文章希望可以留给有需要的人。
不过这几天我又要收假了,七月份才会出新稿,我也不想富奸啊><",真的很希望有兴趣
的同好一起来讨论。Google Calendar API in PHP 教学(三) - 帐号验证
张贴者: Du^2 is my home 2010年6月13日星期日
上篇文章中已教导如何使用api,而Google对於帐号验证方式有以下两种:
(如果对於验证没有什麽兴趣的朋友可直接跳过)
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 = '
[email protected]'; //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的状况。
(如果有人知道为何会发生错误,欢迎一起来讨论。)
後记:
这边文章希望可以留给有需要的人。
不过这几天我又要收假了,七月份才会出新稿,我也不想富奸啊><",真的很希望有兴趣
的同好一起来讨论。
--
猜透女生的心意,比处理别人的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