每个文件的代码分别如下
01 //add_server.php
02 include_once'config.inc.php';
03 include_once'../library/OAuthStore.php';
04
05 $store= OAuthStore::instance('MySQL',$dbOptions);
06
07 // 当前用户的ID, 必须为整数
08 $user_id= 1;
09
10 // 服务器描述信息
11 $server=array(
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'
19 );
20
21 // 将服务器信息保存在 OAuthStore 中
22 $consumer_key=$store->updateServer($server,$user_id);
平板视图
打印?
01 //authorize.php
02 /*session_start();
03
04 if (empty($_SESSION['authorized']))
05 {
06 $uri = $_SERVER['REQUEST_URI'];
07 header('Location: /login.php?goto=' . urlencode($uri));
08 exit();
09 }*/
10
11 include_once'config.inc.php';
12 include_once'../library/OAuthStore.php';
13 include_once'../library/OAuthServer.php';
14
15 //登陆用户
16 $user_id= 1;
17
18 // 取得 oauth store 和 oauth server 对象
19 $store= OAuthStore::instance('MySQL',$dbOptions);
20 $server=newOAuthServer();
21
22 try
23 {
24 // 检查当前请求中是否包含一个合法的请求token
25 // 返回一个数组, 包含consumer key, consumer secret, token, token secret 和 token type.
26 $rs=$server->authorizeVerify();
27 print_r($rs);
28 if($_SERVER['REQUEST_METHOD'] =='POST')
29 {
30 // 判断用户是否点击了 "allow" 按钮(或者你可以自定义为其他标识)
31 $authorized=array_key_exists('allow',$_POST);
32
33 // 设置token的认证状态(已经被认证或者尚未认证)
34 // 如果存在 oauth_callback 参数, 重定向到客户(消费方)地址
35 $server->authorizeFinish($authorized,$user_id);
36
37 // 如果没有 oauth_callback 参数, 显示认证结果
38 // ** 你的代码 **
39 echo'Success';
40 }
41 else
42 {
43 echo'Error';
44 }
45 }
46 catch (OAuthException$e)
47 {
48 // 请求中没有包含token, 显示一个使用户可以输入token以进行验证的页面
49 // ** 你的代码 **
50 }
平板视图
打印?
1 //config.inc.php
2 // 数据库连接信息
3 $dbOptions=array(
4 'server'=>'localhost',
5 'username'=>'root',
6 'password'=>'admin',
7 'database'=>'oauth'
8 );
平板视图
打印?
01 //index.php
02
03 <?php
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';
08
09 $store= OAuthStore::instance('MySQL',$dbOptions);
10
11 // 用户Id, 必须为整型
12 $user_id= 1;
13
14 // 消费者key
15 $consumer_key='eb4fddcdd368842837a55d9082e652b904f465adb';
16
17 // 从服务器获取未授权的token
18 $token= OAuthRequester::requestRequestToken($consumer_key,$user_id);
19 var_dump($token);
20 die();
21 }
22 else{
23 ?>
24 <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
25 <html xmlns="http://www.w3.org/1999/xhtml">
26 <head>
27 <meta http-equiv="Content-Type"content="text/html; charset=utf-8"/>
28 <title>测试页面</title>
29 </head>
30
31 <body>
32 <p>消费放测试页面,点击下面的按钮开始测试</p>
33 <input type="button"name="button"value="Click Me"id="RequestBtn"/>
34 <script type="text/javascript">
35 document.getElementById('RequestBtn').onclick =function(){
36 window.location ='index.php?req=1';
37 }
38 </script>
39 </body>
40 </html>
41 <?php
42 }
43 ?>
平板视图
打印?
01 //oauth_register.php
02 // 当前登录用户
03 $user_id= 1;
04
05 // 来自用户表单
06 $consumer=array(
07 // 下面两项必填
08 'requester_name'=>'Fising',
09 'requester_email'=>'Fising@admin.com',
10
11 // 以下均为可选
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
19 );
20
21 include_once'config.inc.php';
22 include_once'../library/OAuthStore.php';
23
24 // 注册消费方
25 $store= OAuthStore::instance('MySQL',$dbOptions);
26 $key=$store->updateConsumer($consumer,$user_id);
27
28 // 获取消费方信息
29 $consumer=$store->getConsumer($key,$user_id);
30
31 // 消费方注册后得到的 App Key 和 App Secret
32 $consumer_id=$consumer['id'];
33 $consumer_key=$consumer['consumer_key'];
34 $consumer_secret=$consumer['consumer_secret'];
35
36 // 输出给消费方
37 echo'Your App Key: '.$consumer_key;
38 echo'
39 ';
40 echo'Your App Secret: '.$consumer_secret;
平板视图
打印?
01 //request_token.php
02 include_once'config.inc.php';
03 include_once'../library/OAuthStore.php';
04 include_once'../library/OAuthServer.php';
05
06 $store= OAuthStore::instance('MySQL',$dbOptions);
07
08 $server=newOAuthServer();
09 $server->requestToken();
10 exit();
平板视图
打印?
01 /**
02 * Installs all tables in the mysql.sql file, using the default mysql connection
03 */
04
05 /* Change and uncomment this when you need to: */
06
07 /*
08 mysql_connect('localhost', 'root');
09 if (mysql_errno())
10 {
11 die(' Error '.mysql_errno().': '.mysql_error());
12 }
13 mysql_select_db('test');
14 */
15
16 $sql=file_get_contents(dirname(__FILE__) .'/mysql.sql');
17 $ps=explode('#--SPLIT--',$sql);
18
19 foreach($psas$p)
20 {
21 $p= preg_replace('/^\s*#.*$/m','',$p);
22
23 mysql_query($p);
24 if(mysql_errno())
25 {
26 die(' Error '.mysql_errno().': '.mysql_error());
27 }
28 }