-
四種角色
資源擁有者(Resource Owner)
: 通常是自己個人資源服務器(Resource Server)
: 數據信息在該服務器上保存, 使用API接口攜帶token請求該服務器應用(Client)
: 指的第三方應用授權服務器(Authorization Server)
: 返回令牌token的服務器, 一般和資源服務器是相同的
-
grant_type分爲幾種:
- Resource owner password-based --密碼式
- 高度信任應用(高危險)
- POST方法請求
- 參數需要:賬戶(
username
)和密碼(password
) - 請求token返回json值中包含token
- Authorization code – 授權碼(QQ第三方好像用的是這個;鏈接)
- 四種中安全性最高
- GET請求
- 參數需要
3.1client_id
: app應用憑證
3.2scope
: 可選參數; 對此授權權限
3.3response_type
: code [固定值]
3.4redirect_uri
: 即將跳轉的網址 - 返回code: 在redirect_uri後邊加上?code=xxx
- 後端根據code去獲取token
- Implicit – 隱藏式
- 不安全(token暴露在前端)
- GET請求
- 參數需要
3.1client_id
: app應用憑證
3.2scope
: 可選參數; 對此授權權限
3.3response_type
: token [固定值]
3.4redirect_uri
: 即將跳轉的網址 - 返回url後攜帶: access_token等信息
- Client credentials – 客戶端憑證
- 不安全
- GET請求
- 參數
3.1.client_id
: app的id
3.2.client_secret
: app的祕鑰
3.3.grant_type
: client_credentials[固定值]
3.4.scope
: 權限(可選參數)
存在的問題
主要說一下curl API時候的存在的問題 !
#官方描述
curl -X POST -d "grant_type=password&username=<user_name>&password=<password>" -u"<client_id>:<client_secret>" http://localhost:8000/o/token/
- username --是註冊的郵箱賬號,不是姓名
否則會出現–{"error_description": "Invalid credentials given.", "error": "invalid_grant"}
-
I. 當grant_type設置爲"Resource owner password-based",則在curl的時候,grant_type=password,而且"Client type"需要設置爲"public"curl -H "Authorization: Bearer access_token" http://localhost:8000/users/
否則,報錯–
II. 當grant_type設置爲"Client credentials",{"error": "invalid_client"}
curl -X POST -d "grant_type=client_credentials" -u"<client_id>:<client_secret>" http://localhost:8000/o/token/
- client_id --這個是創建時候自己定義的。
#另外一種寫法 curl -X POST http://localhost:8000/o/token/ -d "[email protected]&password=admin123&grant_type=password&client_id=<client_id>" #備註:基本所有的參數都能通過 ‘&變量=’ 添加上,相當於傳遞表單