JavaScript 从 URL 中获取 OAuth 令牌

JavaScript grab OAuth token from URL?

本文关键字:OAuth 令牌 获取 URL JavaScript      更新时间:2023-09-26

我正在使用一个隐式使用 OAuth 的 API。它会重定向到 URL http://exmaple.com/#token=084758yhroufgbk48y。

我如何通过JavaScript获取它?谢谢

您可以使用

window.location.hash获取URL的哈希部分

要从 url 获取令牌,请使用以下代码:

<script type="text/javascript">
    hash = window.location.hash.substr(1); //url of the current page
    arHash = hash.split('='); //this creates an array with key ([0] element) and value ([1] element)
    hash_value =  arHash[1]; //recieve value
</script>