导航链接
(1)香橙派+apache2与php+天猫精灵=自建平台语音支持--前言
(2)香橙派+apache2与php+天猫精灵=自建平台语音支持--香橙派操作系统安装
(3)香橙派+apache2与php+天猫精灵=自建平台语音支持--香橙派环境配置
(4)香橙派+apache2与php+天猫精灵=自建平台语音支持--apache2与php
(5)香橙派+apache2与php+天猫精灵=自建平台语音支持--MariaDB的安装
(6)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接1
(7)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接2
(8)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接3
(9)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接4
(10)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接5
(11)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接6
(12)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接7
继续
好知道了我们要做的事,下边就是按照教程进行服务器的搭建了,首先我们要下载环境包,这里我们要把这个东东安装在我们apache的主目录下默认是/var/www/html,我们要首先进去这个目录
rtplay@orangepipcplus:~$ cd /var/www/html/
然后下载所需要的文件
rtplay@orangepipcplus:/var/www/html/$ git clone https://github.com/bshaffer/oauth2-server-php.git -b master
下面就是要建立数据库了,先建立database 然后在创建tables,首先进入数据库
rtplay@orangepipcplus:/var/www/html$ sudo mysql -u root
进入之后建议先创建一个用户,因为在后面需要给访问的用户设置密码,给root设置密码的时候发现会进入一种未知领域,就是设置密码成功但是还进不去的程度,这时就只能卸载mysql,重新再来。
所以建议新建一个用户,给这个用户创建密码,即使后来忘记了也可以通过root来进行修改。
创建用户tmalk,并且password是需要设置的密码,在后面写接入php的时候需要用到,一定要记住。
MariaDB [(none)]> CREATE USER 'tmalk'@'localhost' IDENTIFIED BY 'password';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'tmalk'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
然后需要绑定所有地址:
根据你的系统,有可能是以下两个路径中的一个
/etc/mysql/mariadb.conf.d/50-server.cnf
或者
/etc/mysql/mysql.conf.d/mysqld.cnf
然后注视掉
#bind-address = 127.0.0.1
退出并重新启动服务
MariaDB [(none)]>exit
rtplay@orangepipcplus:/var/www/html$ service mysql restart
创建database,自己创建的这个名字要记住,接下来有用
MariaDB [(none)]> create database tmalk;
进入database
MariaDB [(none)]>use tmalk
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
根据下面的内容直接创建tables,我就不在这一一输入了,按照教程图片里的输入就行
CREATE TABLE oauth_clients (
client_id VARCHAR(80) NOT NULL,
client_secret VARCHAR(80),
redirect_uri VARCHAR(2000),
grant_types VARCHAR(80),
scope VARCHAR(4000),
user_id VARCHAR(80),
PRIMARY KEY (client_id)
);
CREATE TABLE oauth_access_tokens (
access_token VARCHAR(40) NOT NULL,
client_id VARCHAR(80) NOT NULL,
user_id VARCHAR(80),
expires TIMESTAMP NOT NULL,
scope VARCHAR(4000),
PRIMARY KEY (access_token)
);
CREATE TABLE oauth_authorization_codes (
authorization_code VARCHAR(40) NOT NULL,
client_id VARCHAR(80) NOT NULL,
user_id VARCHAR(80),
redirect_uri VARCHAR(2000),
expires TIMESTAMP NOT NULL,
scope VARCHAR(4000),
id_token VARCHAR(1000),
PRIMARY KEY (authorization_code)
);
CREATE TABLE oauth_refresh_tokens (
refresh_token VARCHAR(40) NOT NULL,
client_id VARCHAR(80) NOT NULL,
user_id VARCHAR(80),
expires TIMESTAMP NOT NULL,
scope VARCHAR(4000),
PRIMARY KEY (refresh_token)
);
CREATE TABLE oauth_users (
username VARCHAR(80),
password VARCHAR(80),
first_name VARCHAR(80),
last_name VARCHAR(80),
email VARCHAR(80),
email_verified BOOLEAN,
scope VARCHAR(4000),
PRIMARY KEY (username)
);
CREATE TABLE oauth_scopes (
scope VARCHAR(80) NOT NULL,
is_default BOOLEAN,
PRIMARY KEY (scope)
);
CREATE TABLE oauth_jwt (
client_id VARCHAR(80) NOT NULL,
subject VARCHAR(80),
public_key VARCHAR(2000) NOT NULL
);
输入完我们看一下,有没有我们输入的内容,看到下面的内容就证明我们的数据库建立完成了。
MariaDB [tmalk]> show tables;
+---------------------------+
| Tables_in_tmalk |
+---------------------------+
| oauth_access_tokens |
| oauth_authorization_codes |
| oauth_clients |
| oauth_jwt |
| oauth_refresh_tokens |
| oauth_scopes |
| oauth_users |
+---------------------------+
7 rows in set (0.00 sec)
下面我们需要创建一个OAuth2的服务文档,这个文档将要被所有申请授权的中断调用,命名这个文档为aligenies_server.php,具体内容如下
<?php
$dsn = 'mysql:dbname=tmalk;host=localhost';
$username = 'tmalk';
$password = 'password';
// error reporting (this is a demo, after all!)
ini_set('display_errors',1);error_reporting(E_ALL);
// Autoloading (composer is preferred, but for this example let's just do this)
require_once('oauth2-server-php/src/OAuth2/Autoloader.php');
OAuth2\Autoloader::register();
// $dsn is the Data Source Name for your database, for exmaple "mysql:dbname=my_oauth2_db;host=localhost"
$storage = new OAuth2\Storage\Pdo(array('dsn' => $dsn, 'username' => $username, 'password' => $password));
// Pass a storage object or array of storage objects to the OAuth2 server class
$server = new OAuth2\Server($storage);
// Add the "Client Credentials" grant type (it is the simplest of the grant types)
$server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
// Add the "Authorization Code" grant type (this is where the oauth magic happens)
$server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage));
?>
着重关注图中“dbname=tmalk”部分,是你刚才在数据库中建立的database的名称就是让记住的那个,username是数据库的用户,如果是按照我建议的使用新用户的话就应该是tmalk,密码是你自己设定的密码,在安装数据库的时候设置的,也让记住的那个。具体剩下的是神马意思,没有研究,有兴趣的可以去看OAuth2的官方文档,我这个是照抄的官方教程的内容,嘿嘿。
我们下一步要做的就是建立一个token控制器,这个文件的作用主要是用于返回OAuth2的Token给客户端,具体这一块怎么工作的我也没有整明白,只要明白是为了获取访问令牌就行了,另外,这个具体的可以参考下图
我们会在/var/www/html/aligenie下建一个aligenies_token.php,内容如下
<?php
require_once __DIR__.'/server.php';
#error_log($_POST['grant_type']);
#error_log($_POST['client_id']);
#error_log($_POST['client_secret']);
#error_log($_POST['code']);
#error_log($_POST['redirect_uri']);
$m=$_POST['redirect_uri'];
$temp = substr($m,0,strpos($m,"?"));
$_POST['redirect_uri']= $temp;
// Handle a request for an OAuth2.0 Access Token and send the response to the client
$server = new OAuth2\Server($storage,array('access_lifetime'=>186400));
$server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage));
$server->addGrantType(new OAuth2\GrantType\RefreshToken($storage,array('always_issue_new_refresh_token' => true)));
$server->handleTokenRequest(OAuth2\Request::createFromGlobals(), new OAuth2\Response())->send();
?>
这一块主要是在调用oauth2生成token响应,响应的数据我抓包下来大概是这样的,有兴趣的朋友可以自己跟一下,也不难。
HTTP/1.1 200 OK\r\nCache-Control: no-store\r\nContent-Type: application/json\r\nPragma: no-cache\r\n\r\n{
"access_token": "b4b72d1e24fd1344dc5a36d1d61540aa12cb22ce",
"expires_in": 186400,
"token_type": "Bearer",
"scope": null,
"refresh_token": "475cca4064ccafa61abb8ef608d47b711c452e0d"
}
然后我们需要在我们刚才搭建的数据库增加一条数据首先我们要先进入我们的数据库tmalk,具体怎么进入见上文,然后输入下文本
MariaDB [tmalk]> INSERT INTO oauth_clients(client_id, client_secret, redirect_uri) VALUES("ID", "PASSWORD", "https://open.bot.tmall.com/oauth/callback");
查看一下有没有添加成功,一定要记住ID和PASSWORD的内容,在接下来会用到,这个就是需要验证和授权的ID和PASSWORD,如果看到如下图的样子,就证明添加完成。
MariaDB [tmalk]> select * from oauth_clients;
+-----------+---------------+-------------------------------------------+-------------+-------+---------+
| client_id | client_secret | redirect_uri | grant_types | scope | user_id |
+-----------+---------------+-------------------------------------------+-------------+-------+---------+
| ID | PASSWORD | https://open.bot.tmall.com/oauth/callback | NULL | NULL | NULL |
+-----------+---------------+-------------------------------------------+-------------+-------+---------+
1 row in set (0.00 sec)
接着往下,我们会创建一个认证控制器,就是天猫精灵设置页面中的授权页面,名字为aligenies_authorize.php如下图所示
<?php
require_once __DIR__.'/server.php';
$request = OAuth2\Request::createFromGlobals();
$response = new OAuth2\Response();
// validate the authorize request
$m=$request->query['redirect_uri'];
$temp = substr($m,0,strpos($m,"?"));
$request->query['redirect_uri']= $temp;
#exit("$m||||$temp");
if (!$server->validateAuthorizeRequest($request, $response)) {
$response->send();
die;
}
// display an authorization form
if (empty($_POST)) {
exit('
<form method="post">
<label>Do You Authorize TestClient?</label><br />
<input type="submit" name="authorized" value="yes">
<input type="submit" name="authorized" value="no">
</form>');
}
// print the authorization code if the user has authorized your client
$is_authorized = ($_POST['authorized'] === 'yes');
$server->handleAuthorizeRequest($request, $response, $is_authorized);
if ($is_authorized) {
// this is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client
$code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5);
$return = urldecode($m)."&code=".$code;
header("Location: ".$return);
//header("Location: ".$m."&code=".$code);
#header("Location: ".$m);
exit("SUCCESS! Authorization Code: $code");
}
$response->send();
?>
正常的认证页面应该要求用户输入用户名和密码,并将用户通过认证后的信息推送到天猫精灵端,这样就可以进行控制了。
这个测试会在协议对接中测试,到这基本上我们需要的文件都已经完事了,主要有aligenies_server.php、aligenies_authorize.php和aligenies_token.php这些文件。
这篇文章主要参考了这两篇文章天猫精灵接入https://bbs.hassbian.com/thread-1912-1-1.html
和https://bbs.hassbian.com/thread-1900-1-1.html。
对于不熟悉PHP开发的亲们来说,小狂提供一个PHP语法和函数的查询网站,挺好用的http://php.net/manual/zh/function.md5.php。