grant_type为client_credentials和pa
最近工作中需要使用到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时,请求的响应中不包含refresh_token:

而grant_type为password则包含refresh_token:

什么时候用client_credentials(客户端模式)作为grant_type?
下面这个网站有介绍:https://www.poken.com/api/authentication/oauth2/
例如Account creation这个API,从业务上说我们期望它可以通过一个未认证过的用户调用,因为在Account创建之前,不存在authenticated user,这种场景下用grant_type=credentails比较合适。

grant_type=password的情况下,请求access token时,需要在HTTP请求里加上client_id和client_secret两个参数。
grant_type = password
客户端需要用户的用户名和密码,然后将客户端自己的client_id和client_secret发送给认证服务器,换取access token. 适用于受信客户端,比如一种服务对应的移动客户端。

grant_type=client_credentials
帖子里举了一个例子,比如实现一个后台作业,调用API完成一些维护工作,这里面不涉及到采集终端用户的用户名和密码,所以用client_credentials比较合适,这种情况下拿Access Token只需要client_id和client_secret即可,不需要用user_id和password了。
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
