grant_type為client_credentials和password二者的區別

i042416發表於2019-11-05

最近工作中需要使用到oauth,注意到oauth客戶端的grant_type值可以指定為client_credentials和password兩種,很好奇所以網上搜尋了一下,發現stackoverflow上一個帖子:
https://stackoverflow.com/questions/34842895/difference-between-grant-type-client-credentials-and-grant-type-password-in-auth

grant_type為client_credentials和password二者的區別

這位網友注意到一個有趣的點:

grant_type為client_credentials和password二者的區別

grant_type=client_credentials時,請求的響應中不包含refresh_token:

grant_type為client_credentials和password二者的區別

而grant_type為password則包含refresh_token:

grant_type為client_credentials和password二者的區別

什麼時候用client_credentials(客戶端模式)作為grant_type?

下面這個網站有介紹: https://www.poken.com/api/authentication/oauth2/
例如Account creation這個API,從業務上說我們期望它可以通過一個未認證過的使用者呼叫,因為在Account建立之前,不存在authenticated user,這種場景下用grant_type=credentails比較合適。

grant_type為client_credentials和password二者的區別

grant_type=password的情況下,請求access token時,需要在HTTP請求里加上client_id和client_secret兩個引數。

grant_type = password

客戶端需要使用者的使用者名稱和密碼,然後將客戶端自己的client_id和client_secret傳送給認證伺服器,換取access token. 適用於受信客戶端,比如一種服務對應的移動客戶端。

grant_type為client_credentials和password二者的區別

grant_type=client_credentials

帖子裡舉了一個例子,比如實現一個後臺作業,呼叫API完成一些維護工作,這裡面不涉及到採集終端使用者的使用者名稱和密碼,所以用client_credentials比較合適,這種情況下拿Access Token只需要client_id和client_secret即可,不需要用user_id和password了。

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

grant_type為client_credentials和password二者的區別


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2662635/,如需轉載,請註明出處,否則將追究法律責任。

相關文章