jfarmer08 / wyze-api

An un-official API wrapper for Wyze products
MIT License
13 stars 5 forks source link

Error when refresh token expires #9

Closed hgoscenski closed 8 months ago

hgoscenski commented 9 months ago

After token expires, it appears that without checking the code/msg of the response the wyze-api is not attempting to refresh the token.

[2/13/2024, 3:08:58 PM] [Wyze Smart home] Refreshing devices...
[2/13/2024, 3:08:58 PM] [Wyze Smart home] Performing request: app/v2/home_page/get_object_list
[2/13/2024, 3:08:58 PM] [Wyze Smart home] Request config: {"method":"POST","url":"app/v2/home_page/get_object_list","data":{"access_token":"<SNIP>","app_name":"com.hualai.WyzeCam","app_ver":"wyze_developer_api","app_version":"wyze_developer_api","phone_id":"wyze_developer_api","phone_system_type":"1","sc":"wyze_developer_api","sv":"wyze_developer_api","ts":1707836938928},"baseURL":"https://api.wyzecam.com"}
[2/13/2024, 3:08:59 PM] [Wyze Smart home] API response PerformRequest: {"code":"2001","msg":"access token is error","traceId":"265862178700f9a907ae2dc979f8daa7","data":{}}
[2/13/2024, 3:08:59 PM] [Wyze Smart home] access token is error
[2/13/2024, 3:08:59 PM] [Wyze Smart home] Error getting devices: TypeError: Cannot read properties of undefined (reading 'length')
jfarmer08 commented 9 months ago

So you think we need to add that back in? I wish I knew all the response returns. @tnoor-co Could you help any with that?

hgoscenski commented 9 months ago

@jfarmer08 I do, my instance was unable to control any accessories and was getting the above message.

The way we are handling recovery from the login failure was via catching the exception, it appears that the Wyze API does not return a failing HTTP code, but rather a 200 OK with a msg/code inside of it.

Looking at the logs I believe that code == 1 indicates success.

jfarmer08 commented 9 months ago

I pushed version 1.1.4

jfarmer08 commented 9 months ago

@hgoscenski Do you mind if I direct message you?

jfarmer08 commented 9 months ago

@hgoscenski looks like result.data.code is null every time. Not sure what is the best way to fix. this.

hgoscenski commented 9 months ago

Interesting, we could potentially do this:

if (result.data.code && result.data.code != 1)

PR: https://github.com/jfarmer08/wyze-api/pull/11

jfarmer08 commented 9 months ago

That does work.

tj-noor commented 9 months ago

@tnoor-co Could you help any with that?

Hey @jfarmer08 - sorry I was a bit swamped today. I did get some time to work on it and was able to gather some error codes and messages we could match on, and then went down a rabbit hole. I initially hoped Wyze was using JWT's, so we could attempt to extract the expiration from it, but the tokens were encrypted - so y'alls current solution of estimating is the most optimal one. I then came across rate limiting headers and added some handling there along with retrying the requests selectively. There were a decent amount of changes made on it that may not be fully on topic here - so apologies in advance for it. Happy to collaborate on it further if you're open to it. https://github.com/jfarmer08/wyze-api/pull/12

jfarmer08 commented 8 months ago

@tnoor-co Thank you for the help with this. I did make a couple of changes, since there were some unexpected ending and the code would not compile. I will do some testing with this, but so far it looks good after the changes.

jfarmer08 commented 8 months ago

I am liking the pull request now. Although I think it needs more testing. Within the next few days I will make a release.