【问题标题】:Implicit OAuth 2.0 Grant Flow possible in Browser for VK.vom?VK.com 的浏览器中可能存在隐式 OAuth 2.0 授权流程?
【发布时间】:2015-02-02 11:19:43
【问题描述】:

我只是在试用 VK.com 的 API,但对他们如何处理身份验证有疑问。

要访问我需要的资源,我的应用程序必须是“独立应用程序”,只允许隐式 OAuth 授权流,并重定向到 vk.com/blank.html。

因此,从服务器端流程中,我无法访问最后附加到其 URL 的令牌。显然,这将适用于(移动)设备,我可以访问较低级别的浏览器,但我有点迷茫,无法弄清楚它如何从独立(或没有,我有一个后端)浏览器/JS应用程序工作。或者是否有可能? 到目前为止,我发现的唯一信息总是使用我没有/不想要的直接用户凭据。我只需要访问附加到重定向到 vk.com/blank.html 的 url 的最终代码,这样我就可以使用它通过后端获取访问令牌(浏览器中没有 client_secret)。

【问题讨论】:

  • 当请求“消息”权限时,回调需要是 vk.com/blank.html 页面,除了可以使用自定义页面。

标签: javascript oauth-2.0


【解决方案1】:

您可以在回调 URL 上提供 Javascript,从 URL 的片段中提取 access_token 并将其 POST 到您的服务器后端。 OpenID Connect 规范中有执行此操作的示例代码,请参阅 http://openid.net/specs/openid-connect-core-1_0.html#FragmentNotes

这是另一个使用 HTML 表单而不是 AJAX 调用的示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
      function postOnLoad() {
        encoded = location.hash.substring(1).split('&');
        for (i = 0; i < encoded.length; i++) {
          encoded[i].replace(/\+/g, " ");
          var n = encoded[i].indexOf("=");
          var input = document.createElement("input");
          input.type = "hidden";
          input.name = decodeURIComponent(encoded[i].substring(0, n));
          input.value = decodeURIComponent(encoded[i].substring(n+1));
          document.forms[0].appendChild(input);
        }
        document.forms[0].action = window.location.href.substr(0, window.location.href.indexOf('#'));
        document.forms[0].submit();
      }
    </script>
    <title>Submitting...</title>
  </head>
  <body onload="postOnLoad()">
    <p>Submitting...</p>
    <form method="post" action=""><p><input type="hidden" name="response_mode" value="fragment"></p></form>
  </body>
</html>

【讨论】:

  • 如果我可以提供回调页面是的,不幸的是 vk.com 要求它是vk.com/blank.html?code=somecode(在那里发出请求时附加代码)。
  • 更正:这只是我试图请求的一个特定权限的情况 - 没有这个权限(我真的不需要),我可以提供回调 url 及其工作正常 - 即使对于可能是服务器端流程。
猜你喜欢
  • 2016-10-15
  • 1970-01-01
  • 2022-01-06
  • 2012-12-27
  • 1970-01-01
  • 2018-08-05
  • 2020-05-17
  • 1970-01-01
  • 2022-01-16
相关资源
最近更新 更多