OAuth2.0不向下兼容1.0版本
四种获得令牌的方式
- 授权码(authorization-code)
- 隐藏式(implicit)
- 密码式(password)
- 客户端凭证(client credentials)
几个重要参数
- response_type:code或token,code需要再次请求获取token
- client_id:客户端身份标识
- client_secret:客户端密钥
- redirect_uri:重定向地址
- scope:授权的范围,read只读,all读写
- grant_type:授权方式
- authorization_code 授权码
- password 密码
- client_credentials 凭证式
- refresh_token 更新令牌
- state:随机数,防止CSRF攻击
1、授权码
安全系数最高,最常用
第一次请求,获取授权码
https://wx.com/oauth/authorize?
response_type=code&
client_id=CLIENT_ID&
redirect_uri=http://juejin.im/callback&
scope=read
微信重定向并带上授权码
http://juejin.im/callback?code=AUTHORIZATION_CODE
第二次请求获取token,需要code和client_secret
https://wx.com/oauth/token?
client_id=CLIENT_ID&
client_secret=CLIENT_SECRET&
grant_type=authorization_code&
code=AUTHORIZATION_CODE&
redirect_uri=http://juejin.im/callback
返回token信息
{
"access_token":"ACCESS_TOKEN",
"token_type":"bearer",
"expires_in":2592000,
"refresh_token":"REFRESH_TOKEN",
"scope":"read",
......
}
2、隐藏式
纯前端应用直接获取token
https://wx.com/oauth/authorize?
response_type=token&
client_id=CLIENT_ID&
redirect_uri=http://juejin.im/callback&
scope=read
3、密码式
需要用户名和密码去申请token
https://wx.com/token?
grant_type=password&
username=USERNAME&
password=PASSWORD&
client_id=CLIENT_ID
4、凭证式
适用于没有前端的应用
https://wx.com/token?
grant_type=client_credentials&
client_id=CLIENT_ID&
client_secret=CLIENT_SECRET
令牌过期
用返回的refresh_token请求,重新获取令牌
https://wx.com/oauth/token?
grant_type=refresh_token&
client_id=CLIENT_ID&
client_secret=CLIENT_SECRET&
refresh_token=REFRESH_TOKEN