Hub 2 error code 500 and bot error result Unauthorized
Analysis
I had two bots connected to hub 2, and was using python to create a program to get temperature and humidity data from hub 2 and a program to run the bots. the API version is v1.0. It was after updating the firmware that the error started to appear.
Expected Behavior
Hub 2 was writing temperature and humidity data to a text file every second and displaying it in a bat file. In the bot, a python file was started when the condition was reached, and it behaved like a switch being pressed with it.
Steps To Reproduce
The only thing that has changed from before is the firmware update.
Logs
-----------Bot---------------
import requests
import json
import tkinter.messagebox as messagebox
auth_key = 'Token'
device_id = 'deviceid'
url = f"https://api.switch-bot.com/v1.1/devices/{device_id}/commands"
headers = {
"Authorization": f"Bearer {auth_key}", # Bearer
"Content-Type": "application/json; charset=utf8"
}
params = {
"command": "turnOff",
"parameter": "default",
"commandType": "command"
}
response = requests.post(url, headers=headers, data=json.dumps(params))
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
if response.status_code == 200:
messagebox.showinfo('shutter', 'shuttered')
else:
messagebox.showerror('Error', f"miss: {response.text}")
~~~~result~~~~
Status Code: 401
Response: {"message":"Unauthorized"}
-----------Hub2-----------------
import requests
import time
from datetime import datetime
# API
API_TOKEN = 'Token'
# deviceID
DEVICE_ID = 'deviceid'
# headers
headers = {
'Authorization': API_TOKEN,
'Content-Type': 'application/json; charset=utf8'
}
# endpointURL
url = f'https://api.switch-bot.com/v1.0/devices/{DEVICE_ID}/status'
file_path = 'humidity.txt'
# dataget
try:
while True:
try:
# send request
response = requests.get(url, headers=headers)
response.raise_for_status
# get responce for json
data = response.json()
# datetime now
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# hub2 data
if 'body' in data and 'humidity' in data['body']:
humidity = data['body']['humidity']
data_str = f'{humidity}\n'
# write file
with open(file_path, 'w', encoding='utf-8') as f:
f.write(data_str)
# print
print(f'{humidity} %')
else:
print('Humidity data could not be obtained.')
except requests.exceptions.RequestException as e:
print(f'A request error has occurred.: {e}')
# wait
time.sleep(1)
except KeyboardInterrupt:
print("Terminate data acquisition.")
~~~~result~~~~
500 Server Error: Internal Server Error for url: https://api.switch-bot.com/v1.0/devices/{deviceID}/status
↑url destination output
{"message":"Unauthorized"}
Configuration
#
Environment
- OS:Windows 11
- Software:Python 3.7 and Python 3.12
Additional Context
No response
In my case..
- Host api.switch-bot.com:443 was resolved.
- IPv6: (none)
- IPv4: 18.181.53.126, 54.65.120.167, 57.180.197.134
- Trying 18.181.53.126:443...
- Connected to api.switch-bot.com (18.181.53.126) port 443
- schannel: disabled automatic use of client certificate
- ALPN: curl offers http/1.1
- ALPN: server accepted http/1.1
- using HTTP/1.x
GET /v1.0/devices HTTP/1.1 Host: api.switch-bot.com User-Agent: curl/8.9.1 Accept: / Authorization: *******
- Request completely sent off
- schannel: remote party requests renegotiation
- schannel: renegotiating SSL/TLS connection
- schannel: SSL/TLS connection renegotiated < HTTP/1.1 500 Internal Server Error < Date: Tue, 17 Dec 2024 09:16:22 GMT < Content-Type: application/json < Content-Length: 16 < Connection: keep-alive < x-amzn-RequestId: 20ed5a89-6ec1-4519-8cfd-e225df2cd7a3 < x-amzn-ErrorType: AuthorizerConfigurationException < x-amz-apigw-id: C7cndHG9tjMEqng= < {"message":null}* Connection #0 to host api.switch-bot.com left intact