');}// 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, 40);exit("SUCCESS! Authorization Code: $code");}$response->send();}}
在tp5/route/route.php中创建相应路由,post方法和get方法都创建
接下来验证创建的是否成功,通过以下链接去访问,在浏览器中输入以下链接,回车后就会显示一个验证表单,当你点击yes按钮后,如果窗口显示一串字符,那么就表示创建成功了,这串字符就是code,接下来需要通过这个code去获取token 。
3.4 实现token申请方法
在OAuth.php控制器中添加函数token(),代码如下
在tp5/route/route.php中创建相应路由,post方法和get方法都创建
文章插图
在测试是否获取token之前,我们需要在表中加一条数据,可执行如下SQL:
接下来从CMD运行以下内容,注意:code的值需要换成你上一步生成的code
如果成功的话,你应该会得到 token,如下内容
3.5 实现获取
在OAuth.php控制器中添加函数(),代码如下
在tp5/route/route.php中创建相应路由,post方法和get方法都创建
Route::get('authorize', 'OAuth/authorize');Route::post('authorize', 'OAuth/authorize');
接下来验证创建的是否成功,通过以下链接去访问,在浏览器中输入以下链接,回车后就会显示一个验证表单,当你点击yes按钮后,如果窗口显示一串字符,那么就表示创建成功了,这串字符就是code,接下来需要通过这个code去获取token 。
http://localhost/authorize.php?response_type=code&client_id=testclient&state=xyz
3.4 实现token申请方法
在OAuth.php控制器中添加函数token(),代码如下
public function token(){global $server;$dsn= 'mysql:dbname=XXX;host=127.0.0.1';$username = 'root';$password = '';\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));// Handle a request for an OAuth2.0 Access Token and send the response to the client$server->handleTokenRequest(\OAuth2\Request::createFromGlobals())->send();}
在tp5/route/route.php中创建相应路由,post方法和get方法都创建
Route::get('token', 'OAuth/token');Route::post('token', 'OAuth/token');
文章插图
在测试是否获取token之前,我们需要在表中加一条数据,可执行如下SQL:
INSERT INTO oauth_clients (client_id, client_secret, redirect_uri) VALUES ("testclient", "testpass", "http://fake/");
接下来从CMD运行以下内容,注意:code的值需要换成你上一步生成的code
curl -u testclient:testpass http://localhost/token.php -d 'grant_type=authorization_code&code=YOUR_CODE'
如果成功的话,你应该会得到 token,如下内容
{"access_token":"6f05ad622a3d32a5a81aee5d73a5826adb8cbf63","expires_in":3600,"token_type":"bearer","scope":null}
3.5 实现获取
在OAuth.php控制器中添加函数(),代码如下
public function resource(){// include our OAuth2 Server objectglobal $server;$dsn= 'mysql:dbname=XXX;host=127.0.0.1';$username = 'root';$password = '';\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));// Handle a request to a resource and authenticate the access tokenif (!$server->verifyResourceRequest(\OAuth2\Request::createFromGlobals())) {$server->getResponse()->send();die;}echo json_encode(array('success' => true, 'message' => 'You accessed my APIs!'));}
- 【独立版】基于ThinkPHP框架开发的智慧表单事务流程CRM系统
- 一 [Python3][django] 后端框架django学习
- 前端框架三巨头年度走势对比:Vue 增长率最高
- grunt基于NodeJS打包压缩JS和CSS
- 在线vscode code
- 前端框架三巨头:React仍是老大,Vue 增长率最高
- 超越 Spring,Spring Boot 跃居为 Java 最强后端框架!
- python接口自动化8-unittest框架使用
- 坎贝尔
- 基于嵌入式GPRS DTU模块在共享按摩椅系统中应用