Working Ebay Oauth Node Express JavaScript

After three frustrating days of trying to get Ebay's oauth to give me an access token, I have finally worked it out. As the docs are pain and there is little to no help online, I have decided to post my solution here in the hope that it will help others. I am no good at stackoverflow so let me know if I need to improve my formatting.

app.get("/login/ebay", (req, res) => {
  res.redirect(
    `https://auth.sandbox.ebay.com/oauth2/authorize?client_id=DeanSchm-TestApp-SBX-b843acc90-fd663cbb&redirect_uri=Dean_Schmid-DeanSchm-TestAp-kqmgc&response_type=code`
  );
});

first thing you need to do is redirect to this url.

The format is like this

https://auth.sandbox.ebay.com/oauth2/authorize?client_id=&redirect_uri=&response_type=code

There is also a scope property, but I don't understand that yet, and I got back a token without is so mehhh.

That url takes you to the ebay login page. If you are using the sandbox, you need to create a sandbox user and login with sandbox credentials.

Once you login, ebay will redirect you a url of your choosing. You enter the url you want to be redirected to here.

Oauth Accepted URL

It's in the ebay developer section under Get A Token From Ebay Via your Application.

This url can be anything. you just have to handle it in node or express or whatever, because as soon as someone signs in that url is where they are heading.

Here is how I handled it

   app.get("/auth/ebay/callback", (req, res) => {
  axios("https://api.sandbox.ebay.com/identity/v1/oauth2/token", {
    method: "post",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      Authorization:
        "Basic " +
        btoa(
          `client public key:client secret keys`
        )
    },
    data: qs.stringify({
      grant_type: "authorization_code",
      // parsed from redirect URI after returning from eBay,
      code: req.query.code,
      // this is set in your dev account, also called RuName

      redirect_uri: "Dean_Schmid-DeanSchm-TestAp-kqmgc"
    })
  })
    .then(response => console.log(response))
    .catch(err => console.log(err));
});

A few gotchas that got me.

  • Make sure you have a space after "Basic " in the authorisation header.

  • bota is a 3rd party library that base 64 encodes your public and secret keys. There are many ways to do this. I just did it this way because I stole a bunch of code.

  • With axios the request body is called data but with fetch and other methods it might be called something else like body or param -The axios method is in a get request because the redirect from ebay defaults to a http get.
  • ebay now uses https.
  • Make sure you are using sandbox urls

Answers:

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.