Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get access token from the url [duplicate]

I'm getting this URL after I'm authenticated to Google

http://localhost:3000/_oauth/google#access_token=ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw&token_type=Bearer&expires_in=3600

How to get access_token value from this url?

I tried solutions in following urls, none of them are working

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

like image 501
user555 Avatar asked Apr 29 '15 04:04

user555


People also ask

How do I get my AUTH access token?

To get a new access token, use the refresh token as you would an authorization code, but with a grant_type value of refresh_token and a refresh_token parameter that holds the contents of the refresh token. The type of grant being used. To exchange a refresh token for an access token, use refresh_token .

How can I get OAuth token URL?

To do so, send a POST request to the OAuth2 Token URL: https://<server>/Panopto/oauth2/connect/token. The post request should be sent with a content type of x-www-form-urlencoded and include the following parameters: grant_type: The method you are using to get a token.

Can access tokens be stolen?

While these tokens are useful for enabling key IT services, they're also vulnerable to theft.


2 Answers

Using modern ways, there are better, more reliable ways to get access token field:

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');

You can also see how this approach makes it easy to get the other parameters in addition. This URLSearchParams approach is supported by all browsers except old instances of IE.


If the above doesn't work (didn't for me) just add 'hash' to window.location, this is also single line code

var access_token = new URLSearchParams(window.location.hash).get('access_token');

Old Answer:

I like RegEx so here's a RegEx answer:

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 is:

ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw

(Directly from the console)

Fiddle

like image 56
Downgoat Avatar answered Oct 01 '22 00:10

Downgoat


Make use of the URL class in JS:

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);
like image 45
Stephen Rodriguez Avatar answered Sep 30 '22 23:09

Stephen Rodriguez