Skip to content

A new way of getting deviceId and localKey #323

Description

@SleepWalker

Hello, I've found out, that an official Smart Life app shows you deviceId. I've tested that deviceId with tuya OpenAPI and it works (at least for my smart sockets). This means, that you can get localKey from an OpenAPI /v1.0/devices/{{device_id}}.

I think, that this may be useful to simplify tuyapi setup workflow.

The new setup will consist of three steps:

  • after creating app on iot.tuya.com and getting key/secret, you need to link your devices, but instead of doing that with @tuyapi/cli, you should "Link device by Tuya Smart App Account":
    image
  • then you can grab id from an app. From the main screen tap on device you interested in, then tap "edit icon" to get to the screen where you can manage/share/rename device. On that screen there is "Device information" menu option, where you can look up for the deviceId:
    image
  • finally request device info from tuya OpenAPI, where you can get localKey value:
    image

Activity

  1. Jaja0624 commented on May 20, 2020

    @Jaja0624

    I tried this. I am getting: There was an error in evaluating the Pre-request Script: TypeError: Cannot read property 'sigBytes' of undefined. when trying to make the get request. I also can't tell what I am suppose to use as schema value when making the request.

  2. codetheweb commented on May 21, 2020

    @codetheweb
    Owner

    This could be really useful @SleepWalker, thank you for researching it.

    tuya-cli link still has issues occasionally for some users, so this could be a great alternative.

    I'll take a look at some point this weekend.

  3. self-assigned this
    on May 21, 2020
  4. etttoutoi commented on May 21, 2020

    @etttoutoi

    Hello.

    i would try your method, but im noob . It's ok for my virtual id, i have found.

    But can you tell me how i have to find local key.

    I don't know how i can use tuya openapi.

    Can you tell me step by step what i have to write to found please.

    thank you

  5. Moudoux commented on May 23, 2020

    @Moudoux

    @etttoutoi This quick start guide from tuya has all the info you need for using postman to get the local key. Basically:

    1. Install postman
    2. Download and import the environment and collection in postman
    3. Set up the variables according to your tuya developer profile client_id, device_id (from the app as described in this post), schema, and url (region based, i.e. https://openapi.tuyaeu.com for EU)
    4. Run the access_token task in collections in postman
    5. Run the {{url}}/v1.0/devices/{{device_id}} request to get your local_key
  6. SleepWalker commented on May 23, 2020

    @SleepWalker
    Author

    Here is a gist, that demonstrates how to get localKey and then tries to use it with a demo code from tuyapi README:

    https://gist.github.com/SleepWalker/18f19679adbfacee6c415d4f6dc218bb#file-readme-md

    I've successfully turned on/off my socket using demo.js from that gist 🎉

  7. changed the title [-]A new way of getting deviceId and localKey (Probably)[/-] [+]A new way of getting deviceId and localKey[/+] on May 23, 2020
  8. codetheweb commented on May 23, 2020

    @codetheweb
    Owner

    @SleepWalker so after looking at this a bit, it seems that while linking your TuyaSmart account to your SaaS instance works fine, there's not a programmatic way to get all device IDs. /devices throws an error, and /devices?schema=TuyaSmart gives a permission error. Did you find a good way to list devices?

    Ideally, the user would be able to link their account through the web interface and then run one tuya-cli command to list devices; without having to manually find and copy device IDs.

  9. SleepWalker commented on May 23, 2020

    @SleepWalker
    Author

    @codetheweb you can use {{url}}/v1.0/users/{{uid}}/devices for that. But, the only way to get uid I know, is by copying deviceId from an app and than get uid field from device info request.

    So the user should copy deviceId from any random device, that he had connected to his account. And then it can be used to list all other devices

    I've added demo script in gist, that lists all the available devices:

    https://gist.github.com/SleepWalker/18f19679adbfacee6c415d4f6dc218bb#file-listdevices-js

  10. Moudoux commented on May 23, 2020

    @Moudoux

    deviceId can be obtained programmatically using the cloudtuya library, by providing it with your account login details (smart_life, tuya smart app) it will list the device ids for all connected devices, which can then be used here.

  11. tim-jc commented on Jun 13, 2020

    @tim-jc

    So close, but so far on this one. Fell at running {url}}/v1.0/devices/{{device_id}} with the error below. The access_token task had run successfully so I have my Postman environment set up correctly. Anybody seen this error?
    image

  12. SleepWalker commented on Jun 13, 2020

    @SleepWalker
    Author

    @tim-jc looks like you've forgot to link your smart life account to your tuya dev account (https://auth.tuya.com/). See the issue description for the screenshots.

  13. aidbish commented on Jun 13, 2020

    @aidbish

    What do i put for the schema.
    i am struggling to get token auth. i get
    {
    "code": 1000,
    "msg": "data not exist",
    "success": false,
    "t": 1592041129210
    }

  14. tim-jc commented on Jun 13, 2020

    @tim-jc

    @SleepWalker - thank you. I had linked, but when I went back in to my Tuya account I saw my device count was now 0. I think I'd inadvertently run the DEL version of {url}}/v1.0/devices/{{device_id}} initially rather than GET. Re-added the device in the Tuya app and was able to get the key. Appreciate the help.

  15. 31 remaining items

  16. 1aranzant commented on Feb 18, 2021

    @1aranzant

    you need to link devices first

    That's the issue, I get the same screen when trying to link a device.

  17. kinstray commented on Feb 18, 2021

    @kinstray

    Yes, I'm getting those ridiculous pricing schemes too. My switch is still working with Octopi. Was it only a 1 month trial of the dev services?

    Got another e-mail you can use maybe?

  18. Senbei123 commented on Feb 19, 2021

    @Senbei123

    Please just use the trial! You only need it for getting the local key.
    If you running home assistant as as example, you can run then the commands with a addon called local tuya without the cloud!

  19. vaderag commented on Mar 2, 2021

    @vaderag

    Could someone please assist.

    I run the access_token - all works great - success = true

    I then paste {{url}}/v1.0/devices/{{device_id}} into the GET box... run it
    { "code": 1010, "msg": "token invalid", "success": false, "t": 1614xxxxxxxxx }

    What am I doing wrong?

    Edit: nevermind. Seems there is a pre-set GET that has that in although i missed it due to all the chinese writing!!

  20. DEVAXTATOR commented on Mar 2, 2021

    @DEVAXTATOR
  21. matyman1122 commented on Mar 4, 2021

    @matyman1122

    I am having trouble getting the API Explorer to work. I have linked device:
    image
    I have tried the different regions as well China gets permission denied:
    image
    Europe returns blank response
    image
    My devices show in the device list under Europe selection
    image

    Anyone have ideas here? I need to create an account on the app under a different locations?

  22. DEVAXTATOR commented on Mar 4, 2021

    @DEVAXTATOR

    I am having trouble getting the API Explorer to work. I have linked device:
    image
    I have tried the different regions as well China gets permission denied:
    image
    Europe returns blank response
    image
    My devices show in the device list under Europe selection
    image

    Anyone have ideas here? I need to create an account on the app under a different locations?

    again....
    go to api managment
    api groups
    and grant permissions to the api only DEVICE MANAGMENT wait 10 minutes
    and then get device details is that easy
    Captura de ecrã 2021-03-04 125559

  23. matyman1122 commented on Mar 4, 2021

    @matyman1122

    Clearly from my screenshot, device management is enabled and open under the API Groups.
    I think it is strange I am getting blank response from Europe region api?
    Do you get blank response from any region?

  24. DEVAXTATOR commented on Mar 4, 2021

    @DEVAXTATOR
  25. matyman1122 commented on Mar 4, 2021

    @matyman1122

    thanks for the confirmation, I will try again tomorrow

  26. kinstray commented on Mar 4, 2021

    @kinstray

    If you are getting 'Permission Deny' read my post from the 5th Jan 2021...

    #323 (comment)

  27. matyman1122 commented on Mar 4, 2021

    @matyman1122

    If you are getting 'Permission Deny' read my post from the 5th Jan 2021...

    #323 (comment)

    Thanks for the reply, this step is already completed as per my last message. You can see in my screenshot all the API groups are available for selection. I suspect I get the deny message because that deviceID is not available on the China API as my devices are under the Europe location. But when I use the Europe one I get {} response. I might need to raise a ticket to them to enquire the status of the API. I was not able to find a status page anywhere.

  28. matyman1122 commented on Mar 4, 2021

    @matyman1122

    Just to let everyone know I decided to try and use postman to call the Europe API instead and this is working.
    The issue is on the Tuya Cloud not having permissions to the API

  29. matyman1122 commented on Mar 8, 2021

    @matyman1122

    For anyone that is trying to use the API Explorer through Tuya IOT Platform and getting the response {} I have found the issue.
    This is because the connection is getting blocked due to CORS restrictions. I found this error in the developer console.
    image

    I was able to get the API working again using this extension for Chrome:
    https://chrome.google.com/webstore/detail/cross-domain-cors/mjhpgnbimicffchbodmgfnemoghjakai

  30. Repository owner locked and limited conversation to collaborators on Mar 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions