如何从 url 获取访问令牌
How to get access token from the url
我通过 Google
的身份验证后收到此 URL
http://localhost:3000/_oauth/google#access_token=ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw&token_type=Bearer&expires_in=3600
如何从这个 url 中获取 access_token
值?
我尝试了以下 url 的解决方案,其中 none 有效
How can I get query string values in JavaScript?
How to get the value from the GET parameters?
Using the GET parameter of a URL in JavaScript
利用JS中的URL class:
var token = new URL("http://localhost:3000/_oauth/google#access_token=ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw&token_type=Bearer&expires_in=3600").hash.split('&').filter(function(el) { if(el.match('access_token') !== null) return true; })[0].split('=')[1];
alert(token);
使用现代方式,有更好、更可靠的方法来获取访问令牌字段:
var urlString = 'http://localhost:3000/_oauth/google#access_token=ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw&token_type=Bearer&expires_in=3600';
var url = new URL(urlString);
// OR: If you want to use the current page's URL
var url = window.location;
var access_token = new URLSearchParams(url.search).get('access_token');
您还可以看到这种方法如何使获取其他参数变得容易。这种 URLSearchParams
方法受所有浏览器支持,IE 的旧实例除外。
如果上述方法不起作用(对我不起作用)只需将'hash'添加到window.location
,这也是单行代码
var access_token = new URLSearchParams(window.location.hash).get('access_token');
旧答案:
我喜欢 RegEx,所以这是一个 RegEx 答案:
var url = 'http://localhost:3000/_oauth/google#access_token=ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw&token_type=Bearer&expires_in=3600',
access_token = url.match(/\#(?:access_token)\=([\S\s]*?)\&/)[1];
access_token
是:
ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw
(直接从控制台)
Fiddle
我通过 Google
的身份验证后收到此 URLhttp://localhost:3000/_oauth/google#access_token=ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw&token_type=Bearer&expires_in=3600
如何从这个 url 中获取 access_token
值?
我尝试了以下 url 的解决方案,其中 none 有效
How can I get query string values in JavaScript?
How to get the value from the GET parameters?
Using the GET parameter of a URL in JavaScript
利用JS中的URL class:
var token = new URL("http://localhost:3000/_oauth/google#access_token=ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw&token_type=Bearer&expires_in=3600").hash.split('&').filter(function(el) { if(el.match('access_token') !== null) return true; })[0].split('=')[1];
alert(token);
使用现代方式,有更好、更可靠的方法来获取访问令牌字段:
var urlString = 'http://localhost:3000/_oauth/google#access_token=ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw&token_type=Bearer&expires_in=3600';
var url = new URL(urlString);
// OR: If you want to use the current page's URL
var url = window.location;
var access_token = new URLSearchParams(url.search).get('access_token');
您还可以看到这种方法如何使获取其他参数变得容易。这种 URLSearchParams
方法受所有浏览器支持,IE 的旧实例除外。
如果上述方法不起作用(对我不起作用)只需将'hash'添加到window.location
,这也是单行代码
var access_token = new URLSearchParams(window.location.hash).get('access_token');
旧答案:
我喜欢 RegEx,所以这是一个 RegEx 答案:
var url = 'http://localhost:3000/_oauth/google#access_token=ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw&token_type=Bearer&expires_in=3600',
access_token = url.match(/\#(?:access_token)\=([\S\s]*?)\&/)[1];
access_token
是:
ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw
(直接从控制台)