jfarmer08 / homebridge-wyze-smart-home

Wyze Smart Home plugin for Homebridge.
MIT License
99 stars 17 forks source link

WYZE Refresh Tokens #258

Open mikl730 opened 1 month ago

mikl730 commented 1 month ago

Refresh tokens fail to be accepted by wyze servers after about 1-hour and 45 minutes. See the discussion in attached projects/forum for more details.

https://github.com/SecKatie/ha-wyzeapi/issues/612 https://forums.wyze.com/t/refresh-tokens-not-working/303714/2

Just posting to prevent anyone from wasting hours like I did trying to fix this issue.

github-actions[bot] commented 1 month ago

Message that will be displayed on users' first issue

mikl730 commented 1 month ago

I spent the evening hack together a fix. First let me say I have never worked in this language before and I'm a network engineer that normally Purposely stays away from code but I want my freaking automations to work at home so I sucked it up lol.

Here's what changes I made to index.js in /var/lib/homebridge/node_modules/homebridge-wyze-smart-home/node_modules/wyze-api/src Added a variable at the top

this.failure = "";

Changed refresh interval to 60 minutes because wyze is expiring them in 1.5 hours right now

setInterval(this.refreshToken.bind(this), 3600);

Edited the 2001 test to add it setting that var

 if   (code === 2001 ||
        errorMessage.toLowerCase().includes("accesstokenerror") ||
        errorMessage.toLowerCase().includes("access token is error")
        )
    {
        this.access_token = "";
        await this.refreshToken().catch((err) => {
        this.failure = "true";
        });

Added another if check below for the var then deleted the token file

     if (this.failure = true)   
    {
        const tokenPath = this._tokenPersistPath();         
    fs.unlink(tokenPath);
    throw new Error(`Oh no 2002 failure refresh token is bad. Erased the stupid file`);
    this.failure = false;
        };
dlewanda commented 1 month ago

@mikl730 Thanks for posting your hack. I am a software developer by trade but haven't done Javascript or Node.js in many years. I am seeing the same issue as you. If you can confirm your fix is continuing to work long term, I'm happy to figure out how to get a PR together that captures your fix to be merged into a release of this repo. I've been a long-time user and would like to give back, and I also really want to get my setup working.

mikl730 commented 1 month ago

Yes it’s still working great

On Sat, Jul 20, 2024 at 1:58 PM Dave Lewanda @.***> wrote:

@mikl730 https://github.com/mikl730 Thanks for posting your hack. I am a software developer by trade but haven't done Javascript or Node.js in many years. I am seeing the same issue as you. If you can confirm your fix is continuing to work long term, I'm happy to figure out how to get a PR together that captures your fix to be merged into a release of this repo. I've been a long-time user and would like to give back, and I also really want to get my setup working.

— Reply to this email directly, view it on GitHub https://github.com/jfarmer08/homebridge-wyze-smart-home/issues/258#issuecomment-2241245038, or unsubscribe https://github.com/notifications/unsubscribe-auth/BJ3NKNFBJPNLPPUORYH4D6DZNKQMZAVCNFSM6AAAAABK3L7IROVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBRGI2DKMBTHA . You are receiving this because you were mentioned.Message ID: @.***>

hartyparty commented 1 month ago

I spent the evening hack together a fix. First let me say I have never worked in this language before and I'm a network engineer that normally Purposely stays away from code but I want my freaking automations to work at home so I sucked it up lol.

Here's what changes I made to index.js in /var/lib/homebridge/node_modules/homebridge-wyze-smart-home/node_modules/wyze-api/src Added a variable at the top

this.failure = "";

Changed refresh interval to 60 minutes because wyze is expiring them in 1.5 hours right now

setInterval(this.refreshToken.bind(this), 3600);

Edited the 2001 test to add it setting that var

 if   (code === 2001 ||
        errorMessage.toLowerCase().includes("accesstokenerror") ||
        errorMessage.toLowerCase().includes("access token is error")
          )
  {
        this.access_token = "";
        await this.refreshToken().catch((err) => {
        this.failure = "true";
        });

Added another if check below for the var then deleted the token file

   if (this.failure = true)   
  {
      const tokenPath = this._tokenPersistPath();         
  fs.unlink(tokenPath);
  throw new Error(`Oh no 2002 failure refresh token is bad. Erased the stupid file`);
  this.failure = false;
        };

can you attach a copy of the index.js? I tried to make these changes but get completely yelled at by HomeBridge about this plugin.

AlienInCA commented 1 month ago

Posting here hoping that a solution is available soon.

tbsaunders1 commented 3 weeks ago

i'm having the same issue here, have changed access token and key with same error persisting.

homebridge v1.7.0 node v20.16.0 wyze plugin v0.5.46

[Wyze] Wyze API Error (2001): 'access token is error' [Wyze] Wyze API Error (1001): 'INVALID_PARAMETER'

jfarmer08 commented 2 weeks ago

@hartyparty if you feel your fix will work for other, please feel free to create a pull request.