2

I have a string which and I need to get the access token out of this string .However we also need to confirm if access token is there or not .

The string is as followed .

      ?utm_source=xyz&utm_medium=xyza&utm_campaign=xyzb&access_token=abybsjw16373vdgw 

I need to get the access token out of this using javascript. I would like to understand as how people approach such problems .

dfsdigging
  • 345
  • 1
  • 4
  • 15
  • 2
    Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – imjared Apr 07 '19 at 10:08
  • Do you get just this string or a full url ? – adesurirey Apr 07 '19 at 10:16

3 Answers3

4

we can also do this like

let str="?utm_source=xyz&utm_medium=xyza&utm_campaign=xyzb&access_token=abybsjw16373vdgw"

       let tokenIsPresent=str.search("access_token")
       if(tokenIsPresent !=-1){
       let token= str.slice(tokenIsPresent+13)
        console.log(token)}
        else{console.log("token is not present")}

we can find access_token by string.search which will return starting position of our search string ie "50" here and then we have to add the length of our search ie "access_token=" which is 13 then we have to slice it by 50+13

akshay bagade
  • 1,169
  • 1
  • 11
  • 24
  • 1
    What if the `access_token` length will change? this is generated on backend side so we cannot guarantee that the length will be the same e.g. you can take a look on https://stackoverflow.com/questions/4408945/what-is-the-length-of-the-access-token-in-facebook-oauth2 You can e.g. slice to the end of the string, but we are still not sure if the parameters orders will be the same. We can get index of `access_token`, check if we can find any `&` characters and slice till the `&` and if not found, slice til the end – bsekula Apr 09 '19 at 19:55
  • whatever the length and parameter order, if we got "access_token" at last it will work regardless of tokens length – akshay bagade Apr 11 '19 at 06:39
  • If the access token length will change, to e.g. 14, and you will slice 13 characters you will miss one char at the end – bsekula Apr 11 '19 at 06:45
  • did you asking about "access_token" as a key or its value eg "abybsjw16373vdgw", here access_token=abybsjw16373vdgw, its value should change every time ie "abybsjw16373vdgw" and key ie "access_token" it will remain as it is – akshay bagade Apr 11 '19 at 07:59
  • Yes `access_token` will be the same :) I think the question is how to get value of the access_token – bsekula Apr 11 '19 at 08:21
  • and we are getting it :) – akshay bagade Apr 11 '19 at 10:01
1

You can try:

const str = "?utm_source=xyz&utm_medium=xyza&utm_campaign=xyzb&access_token=abybsjw16373vdgw "
const accessParts = str.match('access_token=.*$')[0].split("=")

console.log(accessParts) // ["access_token", "abybsjw16373vdgw "]
bsekula
  • 914
  • 1
  • 9
  • 25
0
let token='';
if(window.location.href.split('access_token').length>0){
    token=window.location.href.split('access_token')[1].split('&').join(''); 
}
if(!token){
    console.log('token is missing');
}