4

I am making a GET call with the following URL

https://auth.ebay.com/oauth2/authorize?client_id=CLIENT_ID&response_type=code&redirect_uri=REDIRECT_URI&scope=https://api.ebay.com/oauth/api_scope

This URL will redirect me to a "success.php" website from my server. With that redirection, it adds in params to the URL. For example: https://www.example.com/success.php?code=12345.

I need to get that code param from this redirection. How can I do that?

I tried to do a basic .get() call, but it doesnt seem to work..

https.get(url, (resp) => {
  let data = '';

  // A chunk of data has been received.
  resp.on('data', (chunk) => {
    data += chunk;
  });

  // The whole response has been received. Print out the result.
  resp.on('end', () => {
    console.log(JSON.parse(data).explanation);
  });

}).on("error", (err) => {
  console.log("Error: " + err.message);
});

I have tried other ways that I thought would work from research on here, like waiting for the redirect, etc. Nothing seems to work.

newJavaCoder
  • 93
  • 1
  • 8
  • Does this answer your question? [Execute a function after changing location.href](https://stackoverflow.com/questions/37716183/execute-a-function-after-changing-location-href) – Mohammad Hossein Dolatabadi Dec 30 '21 at 16:09

2 Answers2

0

It is a query param ( seems to me you are working with an oAuth flow, reading about how these flows work would also help you work out an approach to this)

So I would capture it the following way

app.get('/callback', function (req, res) {

   var code = req.query.code || null;
   console.log(code);

}

where /callback is the URL you are redirected to and where you can capture the code to request the authorization token

Jordi Riera
  • 159
  • 4
  • but i need to call the ebay link to be redirected. if I do into my URL, it wont provide me the code... – newJavaCoder Dec 30 '21 at 21:31
  • Yes, you need to call that URL from your server and that URL will redirect back to your server once the credentials are provided. But that's not what you asked, you asked how to capture the code parameter in your original question. That said, In your initial request you normally provide a client id and a secret id and a redirectURi in the headers or body of the request (the specs of the API will tell you that) and that's how the URL redirects back to your callback url, where you capture the code parameter as I showed above. – Jordi Riera Jan 01 '22 at 13:43
0

Based on the code you already have it seems you might want the following.

var https = require('https');
var url = 'https://auth.ebay.com/oauth2/authorize?client_id=CLIENT_ID&response_type=code&redirect_uri=REDIRECT_URI&scope=https://api.ebay.com/oauth/api_scope';
https.get(url, (resp) => {
  
  var location = new URL(resp.headers.location);
 
  var code = location.searchParams.get('code');
  
  console.log(code);
  
}).on("error", (err) => {
  console.log("Error: " + err.message);
});

My answer assumes you are writing the code that is making the request while @Jordi Riera assumes you are writing code to process the request. Might you tell us which it is?

  • I get an invalid URL on this for the var location line. – newJavaCoder Dec 30 '21 at 21:30
  • I have not tested this code and I don’t know much about your particular use case. It’s possible that there is no location header though I don’t know how else it would be redirecting. You could do a ```console.log(resp.headers.location)``` to see what is there. – Darryl Martin Dec 31 '21 at 23:01