作者:mobiledu2502858703 | 来源:互联网 | 2013-07-22 14:18
详细的应用场景可以参见百度百科的说明oAuth,本篇文章叙述如何实现oauth服务架构。首先我们从googlecode下载oauth的php开发包点击进入下载页。oauth的协议流程图如下
详细的应用场景可以参见百度百科的说明oAuth,本篇文章叙述如何实现oauth服务架构。首先我们从google code下载oauth的php开发包点击进入下载页。oauth的协议流程图如下:
oauth协议流程图
下载完成之后我们将解压大www目录,目录结构如下图,同时我们需要把oauth-php/library/store/mysql导入到数据库,通过同级目录下的install.php导入(install.php代码需要做修改详细代码见下面php code)。
目录结构
其中的myOauthServer自己所创建的文件夹,分别创建以下文件:
文件结构
每个文件的代码分别如下
02
|
include_once'config.inc.php';
|
03
|
include_once'../library/OAuthStore.php';
|
05
|
$store= OAuthStore::instance('MySQL',$dbOptions);
|
12
|
'consumer_key'=>'eb4fddcdd368842837a55d9082e652b904f465adb',
|
13
|
'consumer_secret'=>'47415be2a343b74f978f3863faa66a56',
|
14
|
'server_uri'=>'http://auth.service.com/',
|
15
|
'signature_methods'=>array('HMAC-SHA1','PLAINTEXT'),
|
16
|
'request_token_uri'=>'http://auth.service.com/request_token.php',
|
17
|
'authorize_uri'=>'http://auth.service.com/authorize.php',
|
18
|
'access_token_uri'=>'http://auth.service.com/access_token.php'
|
21
|
// 将服务器信息保存在 OAuthStore 中
|
22
|
$consumer_key=$store->updateServer($server,$user_id);
|
04
|
if (empty($_SESSION['authorized']))
|
06
|
$uri = $_SERVER['REQUEST_URI'];
|
07
|
header('Location: /login.php?goto=' . urlencode($uri));
|
11
|
include_once'config.inc.php';
|
12
|
include_once'../library/OAuthStore.php';
|
13
|
include_once'../library/OAuthServer.php';
|
18
|
// 取得 oauth store 和 oauth server 对象
|
19
|
$store= OAuthStore::instance('MySQL',$dbOptions);
|
20
|
$server=newOAuthServer();
|
24
|
// 检查当前请求中是否包含一个合法的请求token
|
25
|
// 返回一个数组, 包含consumer key, consumer secret, token, token secret 和 token type.
|
26
|
$rs=$server->authorizeVerify();
|
28
|
if($_SERVER['REQUEST_METHOD'] =='POST')
|
30
|
// 判断用户是否点击了 "allow" 按钮(或者你可以自定义为其他标识)
|
31
|
$authorized=array_key_exists('allow',$_POST);
|
33
|
// 设置token的认证状态(已经被认证或者尚未认证)
|
34
|
// 如果存在 oauth_callback 参数, 重定向到客户(消费方)地址
|
35
|
$server->authorizeFinish($authorized,$user_id);
|
37
|
// 如果没有 oauth_callback 参数, 显示认证结果
|
46
|
catch (OAuthException$e)
|
48
|
// 请求中没有包含token, 显示一个使用户可以输入token以进行验证的页面
|
04
|
if(isset($_GET['req']) && ($_GET['req'] == 1)){
|
05
|
include_once'config.inc.php';
|
06
|
include_once'../library/OAuthStore.php';
|
07
|
include_once'../library/OAuthRequester.php';
|
09
|
$store= OAuthStore::instance('MySQL',$dbOptions);
|
15
|
$consumer_key='eb4fddcdd368842837a55d9082e652b904f465adb';
|
18
|
$token= OAuthRequester::requestRequestToken($consumer_key,$user_id);
|
08
|
'requester_name'=>'Fising',
|
09
|
'requester_email'=>'Fising@admin.com',
|
12
|
'callback_uri'=>'http://www.demo.com/oauth_callback',
|
13
|
'application_uri'=>'http://www.demo.com/',
|
14
|
'application_title'=>'Online Printer',
|
15
|
'application_descr'=>'Online Print Your Photoes',
|
16
|
'application_notes'=>'Online Printer',
|
17
|
'application_type'=>'website',
|
18
|
'application_commercial'=> 0
|
21
|
include_once'config.inc.php';
|
22
|
include_once'../library/OAuthStore.php';
|
25
|
$store= OAuthStore::instance('MySQL',$dbOptions);
|
26
|
$key=$store->updateConsumer($consumer,$user_id);
|
29
|
$cOnsumer=$store->getConsumer($key,$user_id);
|
31
|
// 消费方注册后得到的 App Key 和 App Secret
|
32
|
$consumer_id=$consumer['id'];
|
33
|
$consumer_key=$consumer['consumer_key'];
|
34
|
$consumer_secret=$consumer['consumer_secret'];
|
37
|
echo'Your App Key: '.$consumer_key;
|
40
|
echo'Your App Secret: '.$consumer_secret;
|
02
|
include_once'config.inc.php';
|
03
|
include_once'../library/OAuthStore.php';
|
04
|
include_once'../library/OAuthServer.php';
|
06
|
$store= OAuthStore::instance('MySQL',$dbOptions);
|
08
|
$server=newOAuthServer();
|
09
|
$server->requestToken();
|
02
|
* Installs all tables in the mysql.sql file, using the default mysql connection
|
05
|
/* Change and uncomment this when you need to: */
|
08
|
mysql_connect('localhost', 'root');
|
11
|
die(' Error '.mysql_errno().': '.mysql_error());
|
13
|
mysql_select_db('test');
|
16
|
$sql=file_get_contents(dirname(__FILE__) .'/mysql.sql');
|
17
|
$ps=explode('#--SPLIT--',$sql);
|
21
|
$p= preg_replace('/^\s*#.*$/m','',$p);
|
26
|
die(' Error '.mysql_errno().': '.mysql_error());
|