hsakoh / switchbot-mqtt

SwitchBot MQTT Home Assistant add-on
MIT License
30 stars 1 forks source link

Not able to start anything #11

Closed thotha closed 6 months ago

thotha commented 6 months ago

Core: 2023.12.4 Supervisor: 2023.12.0 Operating System: 11.2 Frontend: 20231208.2 SwitchBot-Mqtt Current version: 1.0.6 Mosquitto broker Current version: 6.4.0

My SwitchBot-Mqtt config with scrambeld keys and tokens Mqtt: AutoConfig: true Host: "" Port: 1883 Id: "" Pw: "" Tls: false SwitchBot: ApiKey: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ApiSecret: >- BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB EnforceDeviceTypes: [] WebhookService: UseWebhook: true UseNgrok: true NgrokAuthToken: CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC HostUrl: AutoStartServices: false LogLevel: Trace

Failure message in logfile: 22:28:29 fail: SwitchBotMqttApp.Services.MqttCoreService[0] StartAsync failed. System.ApplicationException: {"message":"Unauthorized"} at SwitchBotMqttApp.Logics.SwitchBotApiClient.SendAndEnsureSuccessAsync[TBody](HttpRequestMessage requestMessage, CancellationToken cancellationToken, Int32[] additionalStatus) in C:\github\hsakoh\switchbot-mqtt\src\SwitchBotMqttApp\Logics\SwitchBotApiClient.cs:line 138 at SwitchBotMqttApp.Logics.SwitchBotApiClient.GetSceneList(CancellationToken cancellationToken) in C:\github\hsakoh\switchbot-mqtt\src\SwitchBotMqttApp\Logics\SwitchBotApiClient.cs:line 197 at SwitchBotMqttApp.Services.MqttCoreService.PublishSceneEntities(CancellationToken cancellationToken) in C:\github\hsakoh\switchbot-mqtt\src\SwitchBotMqttApp\Services\MqttCoreService.cs:line 511 at SwitchBotMqttApp.Services.MqttCoreService.StartAsync(CancellationToken cancellationToken) in C:\github\hsakoh\switchbot-mqtt\src\SwitchBotMqttApp\Services\MqttCoreService.cs:line 68

MQTT Broker can be used from Z2M and as well as I can see communitcation with the add-on MQTT Explorer. Any Idea why your add-on doesn't start its MQTT service and thereafter the other two as well?

Thank you for your help and suggestions. Kind regards Thomas

hsakoh commented 6 months ago

From the log, it appears that the SwitchBot API is returning a 401 response code. Given the length of the API key and secret you wrote, did you not enter the secret at the API key and the API key at the secret?

(Also, I would recommend not writing the API key or the secret in the Issue.

hsakoh commented 6 months ago

Oh, the key and secret were scrambled. I missed the description. My bad.

thotha commented 6 months ago

Hello, thank you for your fast feedback. 1.) You've answerd for the security already. 2.) My SwitchBot Token has 92 symbols. In my understanding this is equal to ApiSecret 3.) My SwitchBot Key has 32 symbols. Should be equal to your ApiKey 4.) My NgrokAuthToken has 46 symbols.

Any suggestions?

thotha commented 6 months ago

Hello, thank you for your fast feedback. 1.) You've answerd for the security already. 2.) My SwitchBot Token has 92 symbols. In my understanding this is equal to ApiSecret 3.) My SwitchBot Key has 32 symbols. Should be equal to your ApiKey 4.) My NgrokAuthToken has 46 symbols.

Any suggestions?

The same with new created SwitchBot Token & Key...

hsakoh commented 6 months ago

In my SwitchBot account:

The SwitchBot token consists of 96 characters and is set as the ApiKey. The secret is 32 characters long and is set as the ApiSecret. The NgrokAuthToken is 49 characters long and is set as the NgrokAuthToken. Firstly, could you try swapping the ApiKey and ApiSecret and see what happens?

thotha commented 6 months ago

Well, I did swap as requested and now that was the fault.

Maybe you should use a different word for ApiSecret. This because in my mother language (German) we use "Schlüssel" which translates to "Key". This was the reason for my problem.

Have a nice time and all the best for you, your family, relatives and friends for 2024 Thomas

hsakoh commented 6 months ago

I see. Thanks for the valuable insight. Unfortunately, I cannot change the configuration name to maintain compatibility, but I will add a note to the manual!

Referring to the API specification document, would using terms like ApiToken and ApiSecret make it less prone to misunderstanding? https://github.com/OpenWonderLabs/SwitchBotAPI?tab=readme-ov-file#how-to-sign

thotha commented 6 months ago

I would use "API token" and "API key" as seen in the SwitchBot Cloud add-on. That way it sould be clear as the word "Token" is used in the SwitchBot APP under Developer Option also in "German" and probably in other langauges as well.

Bildschirmfoto 2023-12-28 um 23 33 55