0

I need to fetch authorization code from the URL . It is present as a query string parameters.

When I run the belowo URL

https://XXX.authenticaion.com/oauth/authorize?response_type=code&client_id=sb!t113

It redirects to http://localhost:8080/?code=8wFgU1GJo3

I need to parse the localhost URL and fetch the code.

Please help on how to retrieve the code

Code :

 const url = 'https://XXX.authenticaion.com/oauth/authorize?response_type=code&client_id=sb!t113'
     const config = {
      method: "GET"   
    };
    const response = await fetch(url ,config);

    console.log('Response Text...............'+response.text())
MKN
  • 497
  • 3
  • 8
  • 24

1 Answers1

1

You could use plain js URL web api to create URL object and then get the code value.

const url = 'http://localhost:8080/?code=8wFgU1GJo3'
const code = new URL(url).searchParams.getAll('code')

console.log(code)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
  • But my problem is,its a redirect URL so how do I retrieve after running the first URL. – MKN Mar 15 '20 at 09:28
  • Then you should get url from the localhost after redirection in some way for example you could try `window.location.href`, and then you get query string as in answer. – Nenad Vracar Mar 15 '20 at 09:52