Working Ebay Oauth Node Express JavaScript
Asked Answered
U

0

6

After three frustrating days of trying to get E-bay'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=<your_client_id>&redirect_uri=<your_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 e-bay login page. If you are using the sandbox, you need to create a sandbox user and login with sandbox credentials.

Once you login, e-bay 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 e-bay developer section under Get A Token From e-bay 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 authorization header.

  • bota is a 3rd party library that base64 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 e-bay defaults to a http ge`t.

  • e-bay now uses https.

  • Make sure you are using sandbox urls

Unkempt answered 4/10, 2019 at 4:27 Comment(5)
While I think this info is important, it really is not appropriate to post here as there is no actual question.Habitable
I know what you mean fellow brisbaneight. It's just I spent many many days on this and I want to put a useful resource in a single place for anyone who has the same problem and chances upon it. It answers many questions, and I'll be sure to link back to it as I find them and finish off my problem in the coming days. To be honest stackoverflow kind of failed me here as most the ebay oauth questions were answered poorly.Unkempt
It is okay and even encouraged to answer your own question. I'd recommend writing a simple question such as How do I get an oAuth from ebay's api in Node, then posting what you have written here as an answer instead. If you leave it like this, a nazi moderator might come around and close it, since it's technically against the rules. +1Moidore
That's a good ideaUnkempt
Exactly, got to watch out for those moderators hahaHabitable

© 2022 - 2024 — McMap. All rights reserved.