前面講的三種模式都是面向用戶的;
獲取的 access_token 都有對應的 user ;
今天要講的客戶端模式獲取的 access_token 則沒有對應的 user ;
和 密碼模式 相似的地方在於一般都是用於自己的應用;
不會給第三方開放客戶端模式;
它的應用場景主要是機器或應用之間調用 api 訪問數據;
和 簡化模式 相似的地方在於同樣只有 access_token 沒有refresh_token ;
獲取 Token
直接獲取 Token ;
請求方式: POST
鏈接:http:// oauth.test/oauth/token
請求參數如下:
grant_type: client_credentials
scope: *
grant_type : 固定值為 client_credentials
scope : 要申請的權限
請求中沒有任何關於用戶的信息;
OAuth 協議中也沒有明確要求如何認證;
但是獲取 access_token 的這個接口必須要是受保護的;
協議的示例是使用 Authorization: Basic ;
POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
請求的返回值如下:
{
"token_type": "Bearer",
"expires_in": xxx,
"access_token": "xxx",
}
請求的返回值如下:
token_type 是 token 類型一般是 Bearer ;
expires_in 過期時間
access_token 用於訪問資源的令牌
![OAuth 系列(五)客戶端模式 Client Credentials](http://p2.ttnews.xyz/loading.gif)
閱讀更多 三木益達 的文章