tuya / tuya-iot-python-sdk

Tuya IoT Python SDK for Tuya Open API.
MIT License
117 stars 47 forks source link

Use the documented API method to refresh auth token #51

Closed bpfoster closed 2 years ago

bpfoster commented 2 years ago

When running, if the token expires the client attempts to refresh it using the refresh_token. When attempting to refresh the token (__refresh_access_token_if_need()), the response comes back: {'code': 1011, 'msg': 'token invalid', 'success': False, 't': 1640011165661}

I could find no mention of the /v1.0/iot-03/users/token/ endpoint in the API docs. This page describes a different endpoint. Switching to that API seems to fix the issue.

Fixes #50

Also home-assistant/core#62408 home-assistant/core#61903 home-assistant/core#61854 home-assistant/core#62318 home-assistant/core#61820

skynet01 commented 2 years ago

Hey @zlinoliver any chance you can review this? About 13220 people are having an issue with device states not updating because of this one :)

nmajin commented 2 years ago

I've manually applied this fix and over two hours my status is still working, so this is confirmation so far this is working.

Scope666 commented 2 years ago

I've also manually applied the fix and it's working for me as well... hoping this gets merged soon.

moedaug commented 2 years ago

I've also manually applied this fix, so far so good.

trankillity commented 2 years ago

Have also applied this fix manually and have started properly receiving state changes again.

suttridge commented 2 years ago

I have also applied this fix manually and have now started properly receiving state changes again.

npentell commented 2 years ago

is this just a matter of changing the code in the tuya_iot/openapi.py file? If so, I'd like to give it a try. Can someone direct as to where to find this file? Thanks in advance.

Scope666 commented 2 years ago

is this just a matter of changing the code in the tuya_iot/openapi.py file? If so, I'd like to give it a try. Can someone direct as to where to find this file? Thanks in advance.

Take a look here: https://github.com/home-assistant/core/issues/62408#issuecomment-999648277

trankillity commented 2 years ago

Unfortunately, I am still not seeing the status, but this could be another issue based on the error I am seeing in the logs:

Those log entries are not appearing for me or anyone else who has manually patched the correct files. Could you have not correctly patched them?

zlinoliver commented 2 years ago

Hi guys, sorry for the inconvenience! Thanks @bpfoster for creating this PR, we are still reviewing it and double check if there are any other APIs are used incorrectly. Will release a new version soon.

zlinoliver commented 2 years ago

Thanks for the contribution @bpfoster! We have released the v0.6.4 version, which includes this PR's code, will close it.

bpfoster commented 2 years ago

@zlinoliver v0.6.4 as released does not fix this.

  1. The TO_C_SMART_HOME_REFRESH_TOKEN_API is missing both a leading and trailing /. This results a runtime error:
    requests.exceptions.ProxyError: HTTPSConnectionPool(host='openapi.tuyaus.comv1.0', port=443): Max retries exceeded with url: /tokena123963g38ed65eccf0162cae14fas53 (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 404 No such domain')))

    Note the host is wrong and the URL is wrong. openapi.py:19 should be TO_C_SMART_HOME_REFRESH_TOKEN_API = "/v1.0/token/" instead of TO_C_SMART_HOME_REFRESH_TOKEN_API = "v1.0/token"

...

  1. The request to /v1.0/token/{token} needs to be a GET, not a POST, as per your API documentation and my PR. The code as released results in an error:
    {'code': 1108, 'msg': 'uri path invalid', 'success': False, 't': 1640350136020}

    openapi.py:164 should be response = self.get( instead of response = self.post(

zlinoliver commented 2 years ago

@zlinoliver v0.6.4 as released does not fix this.

  1. The TO_C_SMART_HOME_REFRESH_TOKEN_API is missing both a leading and trailing /. This results a runtime error:
requests.exceptions.ProxyError: HTTPSConnectionPool(host='openapi.tuyaus.comv1.0', port=443): Max retries exceeded with url: /tokena123963g38ed65eccf0162cae14fas53 (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 404 No such domain')))

Note the host is wrong and the URL is wrong. openapi.py:19 should be TO_C_SMART_HOME_REFRESH_TOKEN_API = "/v1.0/token/" instead of TO_C_SMART_HOME_REFRESH_TOKEN_API = "v1.0/token"

... 2. The request to /v1.0/token/{token} needs to be a GET, not a POST, as per your API documentation and my PR. The code as released results in an error:

{'code': 1108, 'msg': 'uri path invalid', 'success': False, 't': 1640350136020}

openapi.py:164 should be response = self.get( instead of response = self.post(

Hi @bpfoster thanks for pointing out the issue, we have released the v0.6.6 version, please help to check if it can resolve your problem.

bpfoster commented 2 years ago

Thanks @zlinoliver - those 2 issues now look resolved in v0.6.6.

aperezva commented 2 years ago

Hi anybody could help me?. I can´t find the Tuya_iot directory and also the file to make the changes, also, another processes or insntructions are not clear to me. I´m still having problems with my status tuya devies, and I would wonderful if anybody coukd give me a detailed process,

Thanks

ulf-melin-sveasolar commented 2 years ago

So, do I have to do something to get this fix into my raspberry pi Home Assistant? It is updated to the latest Core and Supervisor and Operating System as of 2022-03-06, Still the Tuya Integration is not working, all the time it looses contact with the devices (unavailable) and it can be fixed by reloading the Tuya Integration.

JeroenTuinstra commented 2 years ago

I am not sure why this is closed as it clearly is not working. HA 2022.3.2 still has the issue. Also it would be nice to include a little bit an explanation as to how to apply this manually.

trankillity commented 2 years ago

I am not sure why this is closed as it clearly is not working. HA 2022.3.2 still has the issue. Also it would be nice to include a little bit an explanation as to how to apply this manually.

It's closed because it was fixed. If it's happening again, create a new issue referencing this issue. Simple.