哪个更安全,为什么?
它们两个都是安全的,这取决于你使用它的环境。
我不明白为什么一个额外的步骤(交换授权码以获取令牌)在一个工作流程中添加了,当服务器可以直接发行访问令牌时。
很简单。你的客户端不安全。我们来详细看一下。
假设你正在开发针对 Instagram API
的应用程序,因此你向 Instagram
注册你的应用,并定义你需要哪些 API
。 Instagram
将向你提供 client_id
和 client_secret
在你的网站上,你设置了这样一个链接:“来使用我的应用”。点击这个链接,你的 Web 应用程序应该向 Instagram API
发送两个请求。
首先
发送一个请求到 Instagram 认证服务器
,请求参数如下。
1. `response_type` with the value `code`
2. `client_id` you have get from `Instagram`
3. `redirect_uri` this is a url on your server which do the second call
4. `scope` a space delimited list of scopes
5. `state` with a CSRF token.
您没有发送client_secret
,因为您可能不信任客户端(试图使用您的应用程序的用户和/或其浏览器)。 客户端可以查看url或JavaScript并轻松找到您的client_secrect
。 这就是为什么您需要另一步的原因。
您将收到code
和state
。这里的code
是temporary
的,并且没有被保存在任何地方。
然后,您需要从服务器发起second
调用Instagram API
。
1. `grant_type` with the value of `authorization_code`
2. `client_id` with the client identifier
3. `client_secret` with the client secret
4. `redirect_uri` with the same redirect URI the user was redirect back to
5. `code` which we have already received.
由于通话是通过我们的服务器进行的,因此我们可以安全地使用 client_secret
(显示我们的身份)和 code
(显示用户已授予client_id
以使用资源)。
作为响应,我们将获得 access_token
。