# oauth2.0_demo **Repository Path**: ythyty/oauth2.0_demo ## Basic Information - **Project Name**: oauth2.0_demo - **Description**: php oauth2.0 demo 用于快速搭建oauth2.0平台 - **Primary Language**: PHP - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 3 - **Created**: 2022-09-20 - **Last Updated**: 2022-09-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### oauth2.0 说明 1. server.php 服务文件 1. token.php 生成token 1. resource.php 资源接口 1. authorize.php 授权页面 1. OauthClient.php 客户端调用类 1. client.php 调用示列 ### 流程 - step1:登录授权 - step2:跳转到对应的第三方应该获取token - step3:通过token获取对应的信息用户信息 step1:登录授权 http://yii2api.local/authorize.php?response_type=code&client_id=testclient&state=xyz&redirect_uri=http://yii2api.local/client.php step2:跳转到对应的第三方应该获取token ``` POST http://yii2api.local/token.php HTTP/1.1 Host: yii2api.local Connection: keep-alive Content-Length: 153 Postman-Token: fd7d4e63-3b0e-120f-d8c1-5555433ab58f Cache-Control: no-cache Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36 Content-Type: application/x-www-form-urlencoded Accept: */* Accept-Encoding: gzip, deflate Accept-Language: zh-CN,zh;q=0.8 grant_type=authorization_code&code=33de9291d824564bcac3de2f2c2d4c56aaa22d11&client_id=testclient&client_secret=testpass&redirect_uri=http://yii2api.local/client.php HTTP/1.1 200 OK Server: nginx/1.9.5 Date: Sat, 20 Jan 2018 02:42:16 GMT Content-Type: application/json Connection: keep-alive X-Powered-By: PHP/5.6.13 Cache-Control: no-store Pragma: no-cache Vary: Accept-Encoding Content-Length: 171 {"access_token":"f237aba68263e12b37a92b64653c9067ef6a28a6","expires_in":3600,"token_type":"Bearer","scope":null,"refresh_token":"d724dc859ea1c39442837da42128703d38be53cf"} ``` step3:通过token获取对应的信息用户信息 ``` GET http://yii2api.local/resource.php?access_token=c5ced4af8644169da4415604745813e82127065e HTTP/1.1 Host: yii2api.local Connection: keep-alive Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding: gzip, deflate, sdch Accept-Language: zh-CN,zh;q=0.8 HTTP/1.1 200 OK Server: nginx/1.9.5 Date: Sat, 20 Jan 2018 02:47:03 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.13 Vary: Accept-Encoding Content-Length: 50 {"success":true,"message":"You accessed my APIs!"} ``` 参考文档 1.http://bshaffer.github.io/oauth2-server-php-docs/ 2.http://www.rollosay.com/it/%E4%BD%BF%E7%94%A8OAuth-Server-PHP%E5%AE%9E%E7%8E%B0OAuth2%E6%9C%8D%E5%8A%A1