Newan / ioBroker.easee

easee is a wallbox with an REST-API. This adapter for ioBroker can used to connect the wallbox with your home environment
MIT License
21 stars 8 forks source link

AxiosError: Request failed with status code 401 #67

Closed Scotty42 closed 1 year ago

Scotty42 commented 1 year ago

Describe the bug
Repeatedly I recently face following situation and error state in the log: AxiosError: Request failed with status code 401 Still the instace is in green state and claims to be connected. HTTP 401 is thrown while token refresh. As I implemented the API in nodered directly before using this adapter I know this error. It happens when the refresh expire time of 3600ms has passed and while refresh API call to easee cloud the refresh token then has already expired. Only remedy is to get a new token and start refresh cycle from scratch.

To Reproduce
Not immediatly reproducible but happens after repeatedly after some days.

Expected behavior
1) Adapter behavior and state detection wrong. After catching a 401 during token refresh the adapter instance shall be in error state AND re-login not trying to re-use the expired refreh token over and over. That is never gonna work 2) expiresIn calculation should be re-thought. I fail to follow the logic expireTime = Date.now() + (response.data.expiresIn - (polltime * 2)) * 1000; expiresIn is given in ms. Presently 3600. Polling config does not change anything with that. To be on the safe side one could just set expire time to expireTime = Date.now() + (response.data.expiresIn)-500 as all values are given in ms. But that's just my proposal.

Versions:

Additional context
If I had a dev setup for iobroker adapters yet I'd be happy to provide a PR for testing

pmueller99 commented 1 year ago

Same here

Newan commented 1 year ago

It seems lika an change in the API / Token refresh. I will check this

Scotty42 commented 1 year ago

Expiration period was 86400s some time ago it changed to 3600. Let me know if I can help in any way

Scotty42 commented 1 year ago

Testing locally to further pinpoint the refresh issue with let expires = response.data.expiresIn; this.log.info('Token valid for ' + JSON.stringify(expires) + 's'); expireTime = Date.now() + (expires - 500) * 1000; instead of expireTime = Date.now() + (response.data.expiresIn - (polltime * 2)) * 1000;

Newan commented 1 year ago

Describe the bug Repeatedly I recently face following situation and error state in the log: AxiosError: Request failed with status code 401 Still the instace is in green state and claims to be connected. HTTP 401 is thrown while token refresh. As I implemented the API in nodered directly before using this adapter I know this error. It happens when the refresh expire time of 3600ms has passed and while refresh API call to easee cloud the refresh token then has already expired. Only remedy is to get a new token and start refresh cycle from scratch.

To Reproduce Not immediatly reproducible but happens after repeatedly after some days.

Expected behavior

  1. Adapter behavior and state detection wrong. After catching a 401 during token refresh the adapter instance shall be in error state AND re-login not trying to re-use the expired refreh token over and over. That is never gonna work
  2. expiresIn calculation should be re-thought. I fail to follow the logic expireTime = Date.now() + (response.data.expiresIn - (polltime * 2)) * 1000; expiresIn is given in ms. Presently 3600. Polling config does not change anything with that. To be on the safe side one could just set expire time to expireTime = Date.now() + (response.data.expiresIn)-500 as all values are given in ms. But that's just my proposal.

Versions:

  • Adapter version: 1.0.8
  • JS-Controller version: 4.0.24
  • Node version: 20.3.1
  • Operating system: Arch

Additional context If I had a dev setup for iobroker adapters yet I'd be happy to provide a PR for testing

Thanks for the hint. I have just once tried to recreate. Basically you are right, just thought with polltime to calculate this is what more dynamic, but is also worth net really.

Have that times changed, looks if that is enough and works. Thank you!

Newan commented 1 year ago

please reopen if error exist