jasonacox / tinytuya

Python API for Tuya WiFi smart devices using a direct local area network (LAN) connection or the cloud (TuyaCloud API).
MIT License
933 stars 166 forks source link

Error from Tuya server: permission deny #66

Open skorphil opened 2 years ago

skorphil commented 2 years ago

Hi, after inputing client id, secret etc i got error Error from Tuya server: permission deny Is there any way to see what exactly go wrong? All services is added to device on the iot cloud web site.

I subscribed to trial version

skorphil commented 2 years ago

Might be thats the problem - https://developer.tuya.com/en/docs/iot/singnature?id=Ka43a5mtx1gsc

jasonacox commented 2 years ago

Hi @skorphil we updated TinyTuya to handle the new signature requirement. Make sure you are using the latest version. Also, some report an issue with region selection in the Tuya portal - check this out: https://github.com/jasonacox/tinytuya/issues/59

skorphil commented 2 years ago
Screenshot 2021-09-15 at 10 20 36

Hi @jasonacox I have added all the servers in the project on tuya iot web site. I have tried all different regions us, eu, cn, in in tinytuya wizard with no results.

Do i have to create assets(with user and devices) in different regions?

skorphil commented 2 years ago

Well, i've tried different combinations of Asset Server location and Region on tinytuya wizard. Doesn't seem to work - access denied.

Do i need to run tinytuya wizard on machine in the same LAN with the device? I wanna try run it on remote vps from different region - maybe this will make any sense. I still wondering is there a way i can get additional information why access is denied? It might help to find a solution

skorphil commented 2 years ago

I've tried different combinations of regions running wizard from europe VPS. Still have access denied. It need so much time to try every combination because there are 6 server options on tuya iot assets(where i have to manually add new device for each server) and i have 4 choices of regions in tinytuya wizard. So i've tried only some combinations. I do not understand the logic behind access to those regions.

Which development method should i use for tuya project? Custom or Smart Home?

Thats insane ))) this error makes me mad cause it describes nothing in terms of how can i fix it

skorphil commented 2 years ago

Well, i've managed to get local key via API on the tuya web site as described here https://community.home-assistant.io/t/inkbird-ibs-m1-with-ibs-p01/255050/20?u=del13r

Test python code seems to work with my device

jasonacox commented 2 years ago

Hi @skorphil I understand your frustration. If you are still interested in troubleshooting this, I have some ideas.

Do i have to create assets(with user and devices) in different regions?

No, key is that you want to use the region where your tuya devices are registered. Which region do they show up in on the Tuya Developer (iot.tuya.com) portal? Here is what my account shows:

image

The TinyTuya wizard basically does a sequence of curl actions against the Tuya API to get the local key for all of your devices. You can simulate this in the Tuya iot portal:

Here is what we do to get the full list:

1) Get the user id (uid) by using the sample device ID in your collection:

image

Notice the region that is selected. This will match the us, eu, cn, in that the wizard asks and will show up in the curl statement as the hostname (e.g. for the us, curl --request GET "https://openapi.tuyaus.com/v1.0/devices...)

2) Using the user id (uid), ask for a full list of all devices:

image

dlapiduz commented 2 years ago

I've been running into this issue with tinytuya and directly with the API for a couple hours and the only way that I could resolve it was by moving the app and devices from the East US region to the Western US region.

jasonacox commented 2 years ago

That's a great find @dlapiduz ! I see Tuya now has these regions:

Region Endpoint
China Data Center https://openapi.tuyacn.com
Western America Data Center https://openapi.tuyaus.com
Eastern America Data Center https://openapi-ueaz.tuyaus.com
Central Europe Data Center https://openapi.tuyaeu.com
Western Europe Data Center https://openapi-weaz.tuyaeu.com
India Data Center https://openapi.tuyain.com

From: https://developer.tuya.com/en/docs/iot/api-request?id=Ka4a8uuo1j4t4

Currently it only handles this format:

url = "https://openapi.tuya%s.com/v1.0/%s" % (apiRegion,uri)

I will update TinyTuya to handle the new ones:

Region Endpoint
Eastern America Data Center https://openapi-ueaz.tuyaus.com
Western Europe Data Center https://openapi-weaz.tuyaeu.com
axydavid commented 2 years ago

Hello, I'm running into the same problem. this is the response I get: { "code": 1106, "msg": "permission deny", "success": false, "t": 1644162150193 }

jasonacox commented 2 years ago

That means that your datacenter region settings are not correct for your local IP address. Unfortunately, Tuya's regions don't really map correctly to physical regions. Tuya is aggressive about regions. If you call from an IP that looks like it is outside of the region they expect, it will be denied. I would try adding other data centre regions in the Tuya cloud portal to your project and try using those regions in the Wizard.

Take a look at the advice in these issues and see if it helps you: https://github.com/jasonacox/tinytuya/issues/100 https://github.com/jasonacox/tinytuya/issues/96

axydavid commented 2 years ago

I just used the root android method to get my key, way easier.... After than everything went smoothly

jasonacox commented 2 years ago

Excellent! Did you find a good tutorial on that? I would be happy to add that link.

axydavid commented 2 years ago

Basically preferences_global_xxxxxxxx.xml is kind of a log file, and it just happens that in this particular app version the file is totally unencrypted and it also shows the local key used for pairing. No need to do any tuya developer stuff.

Original post: https://sites.google.com/view/randyhomeassistant/tuya-local.

jasonacox commented 2 years ago

Thanks @axydavid - great find! Hopefully this will help others. I assume if you have multiple Tuya devices it will likely be multiple "local" items in that folder that will have their keys.

Not that you need it but if any follow after you without an Android device or just wanting to the the Tuya Cloud to work for them, I created a PDF that might help: Tuya.IoT.API.Setup.pdf

axydavid commented 2 years ago

I assume if you have multiple Tuya devices it will likely be multiple "local" items in that folder that will have their keys.

Precisely!!! The data is stored in JSON format.

Not that you need it but if any follow after you without an Android device or just wanting to the the Tuya Cloud to work for them, I created a PDF that might help: Tuya.IoT.API.Setup.pdf Might come in handy.

I just want to add that you can actually boot an android live cd on your PC in case you don't own an android phone.

jasonacox commented 2 years ago

I love this idea @axydavid . I would love to have a push-button easy way for users to get their local keys without the Tuya IoT dev path. Would it be possible for us to build an automated script (container) to set up a android emulator on your PC, execute the installation and login of the SmartLife app and then grab the JSON data for the local keys?

axydavid commented 2 years ago

In theory, you could modify the 3.3.0 smarthome app so that it copies or displays the local key once the device is paired. That way you wouldn't need root anymore in order to access it. This would be the most realistic approach IMHO.

Another detail worth mentioning is that you could also remove any tracking from the app as well, the app already uses the local control of the devices.

For the PC idea, it would be complicated, since if you run emulated android would the app still be able to see the device since the wifi connection is not direct, but relayed? So the only realistic way would be to have a sort of android liveCD, however I think it's much easier and faster just to use any old android phone.