skip to Main Content

What combination of requests and responses are needed to get an Oauth token from eBay? What is a runame and what headers do I need to keep eBay happy?

2

Answers


  1. Chosen as BEST ANSWER

    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`
      );
    });
    

    The 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 me.

    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 log in, eBay will redirect you to 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 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 of the redirect from ebay defaults to an http get.
    • ebay now uses https. Make sure you are using sandbox URLs

  2. We also had to use JS for the eBay API and solved your mention problem with developing a new Lib. It’s available here. This lib will also automatically try to refresh the token if it’s expires.

    This is how we obtain the oAuth token:

    import eBayApi from 'ebay-api';
    
    const eBay = new eBayApi({
      appId: '-- or Client ID --',
      certId: '-- or Client Secret',
      sandbox: false,
      siteId: eBayApi.SiteId.EBAY_US,
      ruName: '-- eBay Redirect URL name --' //in this case: Dean_Schmid-DeanSchm-TestAp-kqmgc
    });
    
    // This will generate the URL you need to visit
    const url = eBay.oAuth2.generateAuthUrl();
    
    // After grant access, eBay will redirect you to RuName page and set the ?code query.
    // Grab the ?code and get the token with:
    eBay.oAuth2.getToken(code).then((token) => {
      console.log('Token', token);
      ebay.oAuth2.setCredentials(token);
    
      // Now you can make request to eBay API:
       eBay.buy.browse.getItem('v1|382282567190|651094235351')
        .then(item => {
            console.log(JSON.stringify(item, null, 2));
        })
        .catch(e => {
            console.log(e);
        });
    });
    

    Another example with scope can we found here.

    Some hints:

    • with "scope" you tell eBay what you plan to use. You can find the
      Descriptions here, under Sandbox/Production Keys Box. (OAuth
      Scopes)
    • if you use axios you can use the auth config, so you dont’t
      need btoa:
    axios("https://api.sandbox.ebay.com/identity/v1/oauth2/token", {
      // ...
      auth: {
        username: 'appId',
        password: 'certId'
      }
    });
    
    • To use sandbox without https, e.g. localhost, you can setup a redirect on a https site and redirec/pass the code to non-https site.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search