OAuth 系列(四)密碼模式 Resource Owner Password Credentials

密碼模式不同於前面講過的 授權碼 和 簡化模式;

前面的這兩種模式中賬號密碼都是在授權服務器上輸入的;

都有重定向跳轉步驟;

客戶端 Clinet 並不知道用戶賬號和密碼;

而密碼模式則需要 Clinet 使用賬號密碼來獲取 Token ;

雖然 OAuth 協議要求 Clinet 禁止存儲用戶的賬號密碼;

但是大家並不能信任第三方的 Client;

因此密碼模式一般用於自家 Client ;

比如說自家的 APP ;

而不會開放給第三方 Client 使用;

下面的演示跟上篇文章一樣;

使用本地創建的 OAuth 項目做示例;

獲取 Token

直接獲取 Token ;

請求方式: POST

鏈接:http:// oauth.test/oauth/token

請求參數如下:

grant_type: password
username: xxx
password: xxx
scope: user:email

grant_type : 固定值為 password

username : 用戶名

password : 密碼

scope : 要申請的權限

請求的返回值如下:

{
"token_type": "Bearer",
"expires_in": xxx,
"access_token": "xxx",
"refresh_token": "xxx",
}

請求的返回值如下:

token_type 是 token 類型一般是 Bearer ;

expires_in 過期時間

access_token 用於訪問資源的令牌

refresh_token 用於刷新 access_token

刷新 token

當 access_token 過期後我們可以使用 refresh_token 獲取新的 access_token ;

刷新 access_token 的方式如下:

請求類型: POST

請求鏈接: http://oauth.test/oauth/token ;

請求參數如下:

grant_type: refresh_token
refresh_token: xxx
scope: user:email

刷新 token 的返回值跟獲取 token 步驟中的返回值一樣;

OAuth 系列(四)密碼模式 Resource Owner Password Credentials


分享到:


相關文章: