hendt / ebay-api

eBay Node API in TypeScript for Node and Browser with RESTful and Traditional APIs. This library aims to implement all available eBay apis.
https://hendt.gitbook.io/ebay-api
MIT License
141 stars 38 forks source link

Auth token not refreshed #164

Open nrathi opened 3 months ago

nrathi commented 3 months ago

Describe the bug I believe I have a use case where the token should automatically get refreshed, but it is not.

Code

   const response =
      await this.api.trading.GetMyeBaySelling?.(
        {
          SoldList: {
            DurationInDays: 60,
            Include: true,
            OrderStatusFilter: "All",
            Sort: "EndTimeDescending",
          },
        },
        { useIaf: true },
      );

Output

 "error": "{\n  \"stack\": \"EbayApiError: Auth token is hard expired.\\n    at checkEBayResponse (/home/johndoe/myapp/node_modules/.pnpm/ebay-api@8.7.1/node_modules/ebay-api/lib/errors/index.js:187:15)\\n    at XMLRequest.request (/home/johndoe/myapp/node_modules/.pnpm/ebay-api@8.7.1/node_modules/ebay-api/lib/api/traditional/XMLRequest.js:118:46)\\n    at runMicrotasks (<anonymous>)\\n    at processTicksAndRejections (node:internal/process/task_queues:96:5)\\n    at async Traditional.request (/home/johndoe/myapp/node_modules/.pnpm/ebay-api@8.7.1/node_modules/ebay-api/lib/api/traditional/index.js:178:20)\\n    at async Object.GetMyeBaySelling (/home/johndoe/myapp/node_modules/.pnpm/ebay-api@8.7.1/node_modules/ebay-api/lib/api/traditional/index.js:44:24)\",\n  \"message\": \"Auth token is hard expired.\",\n  \"description\": \"Auth token is hard expired, User needs to generate a new token for this application.\",\n  \"meta\": {}\n}",

expected data

This error self-resolves, so I think it has to do with the token not being refreshed, and then some other call down the line does the refresh. https://community.ebay.com/t5/eBay-APIs-Talk-to-your-fellow/get-a-new-auth-token-where-do-i-go-to-get-it/td-p/33655751

Would you like to work on this issue?

nrathi commented 3 months ago

Hey @dantio, do you have time to take this on? I could take a stab at it, I think it's just one line.

dantio commented 3 months ago

We haven’t experienced this error yet, and I’m not sure how to reproduce it. If you can explain to me how to trigger this error message, I will try to fix it. Please go ahead if you have time to fix it. I would appreciate it. 😊

nrathi commented 3 months ago

Here's a repro:

  while (true) {
    try {
        const res = await this.api.trading.GetMyeBaySelling?.(
        {
          SoldList: {
            DurationInDays: 60,
            Include: true,
            OrderStatusFilter: "All",
            Sort: "EndTimeDescending",
          },
        },
        { useIaf: true },
      );
      console.log(res);
      await new Promise((res) => setTimeout(res, 15 * 60 * 1000)); // 15 minutes
    } catch (e) {
      console.log(e);
    }
  }

And eventually, you'll get this error. The token should be refreshed and it isn't:

  error: EbayApiError: Auth token is hard expired.
      at checkEBayResponse (/Users/me/my-app/node_modules/.pnpm/ebay-api@8.7.1/node_modules/ebay-api/lib/errors/index.js:187:15)
      at XMLRequest.request (/Users/me/my-app/node_modules/.pnpm/ebay-api@8.7.1/node_modules/ebay-api/lib/api/traditional/XMLRequest.js:118:46)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async Traditional.request (/Users/me/my-app/node_modules/.pnpm/ebay-api@8.7.1/node_modules/ebay-api/lib/api/traditional/index.js:178:20)
      at async Object.GetMyeBaySelling (/Users/me/my-app/node_modules/.pnpm/ebay-api@8.7.1/node_modules/ebay-api/lib/api/traditional/index.js:44:24) {
    description: 'Auth token is hard expired, User needs to generate a new token for this application.',
    meta: {
      Timestamp: '2024-03-29T15:13:35.709Z',
      Ack: 'Failure',
      Errors: [Object],
      Version: 1271,
      Build: 'E1271_CORE_APISELLINGEXTSVC_R1',
      [Symbol(raw-error)]: [Object]
    }
  },

Changing the "catch" as follows works:

  } catch (e) {
    if ( isEBayAuthTokenExpiredError(e)) {
      await eBayApi.OAuth2.refreshToken();
      return myRecursiveFunction(...);
    }
    throw e;
  }
};

function isEBayAuthTokenExpiredError(err: unknown) {
  return (
    err instanceof EBayError &&
    err.description.includes("Auth token is hard expired")
  );
}
dantio commented 3 months ago

Ah, you are using the Auth token. I see. I'll add this check in the next release. Thank you!

dantio commented 3 months ago

I have published it as 8.7.2-RC.0.

nrathi commented 3 months ago

Thanks! I started using it. Will let you know if there are any issues.