Untitled
plain_text
a month ago
1.6 kB
2
Indexable
Never
import json import time import hashlib import hmac import base64 import uuid import requests # Declare empty header dictionary apiHeader = {} # open token token = 'aee8f0065f6e354bb37aa922e6626fb701440dcaf0eadf48e0ff27288a3994acb8043721ac198cb8d9f2fc315eb4c748' # copy and paste from the SwitchBot app V6.14 or later # secret key secret = '315492f4e2fc092df1c28f6eee442df2' # copy and paste from the SwitchBot app V6.14 or later nonce = uuid.uuid4() t = int(round(time.time() * 1000)) string_to_sign = '{}{}{}'.format(token, t, nonce) string_to_sign = bytes(string_to_sign, 'utf-8') secret = bytes(secret, 'utf-8') sign = base64.b64encode(hmac.new(secret, msg=string_to_sign, digestmod=hashlib.sha256).digest()) print ('Authorization: {}'.format(token)) print ('t: {}'.format(t)) print ('sign: {}'.format(str(sign, 'utf-8'))) print ('nonce: {}'.format(nonce)) #Build api header JSON apiHeader['Authorization']=token apiHeader['Content-Type']='application/json' apiHeader['charset']='utf8' apiHeader['t']=str(t) apiHeader['sign']=str(sign, 'utf-8') apiHeader['nonce']=str(nonce) url = 'https://api.switch-bot.com' getList = '/v1.1/devices' #resp = requests.get(url + getList, headers=apiHeader) #print(resp.headers) #print(resp.content) deviceId = 'D2353334647F' commandApi = '/v1.1/devices/' + deviceId + '/commands' command = { 'commandType': 'command', 'command': 'press', 'parameter': 'default', } resp = requests.post(url + commandApi, json=command, headers=apiHeader) print(resp) print(resp.headers) print(resp.content)