-
四种角色
资源拥有者(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>" #备注:基本所有的参数都能通过 ‘&变量=’ 添加上,相当于传递表单