adumont / tplink-cloud-api

A node.js npm module to remotely control TP-Link smartplugs (HS100, HS110) and smartbulbs (LB100, LB110, LB120, LB130) using their cloud web service (no need to be on the same wifi/lan)
https://itnerd.space
GNU General Public License v3.0
129 stars 44 forks source link

Any info on method:tokenRefresh? #43

Closed cjdshaw closed 3 years ago

cjdshaw commented 3 years ago

It really galls me having to store my credentials in order to periodically obtain a token. After a bit of poking, there seems to be a method "refreshToken", but whatever I pass it, e.g.

curl "https://wap.tplinkcloud.com/" -X POST -d '{"method":"refreshToken","url":"https://wap.tplinkcloud.com","params":{"appType":"Kasa_iOS","token":"'$KASATOKEN'","terminalUUID":"'$KASAUUID'"}}' -H "Content-Type:application/json"

I get {error_code": -20104, "msg": "Parameter doesn't exist"}

Do you have any guesses on what it needs? How did you get this info for "login"? Note that other methods, like "refresh" or "updateToken" return "The method does not exist or is not available" so "refreshToken" is clearly different

cjdshaw commented 3 years ago

Ah, I've worked it out. If you add "refreshTokenNeeded":true to the initial login, you get a refresh token in addition, so curl https://wap.tplinkcloud.com -X POST -d '{"method":"login","url":"https://wap.tplinkcloud.com","params":{"appType":"Kasa_iOS","cloudPassword":"xxx","cloudUserName":"xxx@xxx.com","terminalUUID":"'$KASAUUID'","refreshTokenNeeded":true}}' -H "Content-Type:application/json"

You can then periodically call curl https://wap.tplinkcloud.com -X POST -d '{"method":"refreshToken","params":{"appType":"Kasa_iOS","terminalUUID":"'$KASAUUID'","refreshToken":"'$KASAREFRESHTOKEN'"}}' -H "Content-Type:application/json"

and get a new access token each time. Oddly, you don't get a new refresh token, so it remains to be seen whether that will expire at some point