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.
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 thatbase64
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 orparam
-Theaxios
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