-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_device_id.py
54 lines (44 loc) · 1.17 KB
/
get_device_id.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import time
import hashlib
import hmac
import base64
import uuid
import requests
import pprint
from dotenv import load_dotenv
import os
URL = "https://api.switch-bot.com/v1.1/devices"
def make_secret(secret_key):
secret_key = bytes(secret_key, 'utf-8')
return secret_key
def make_sign(secret_key, t, nonce):
string_to_sign = '{}{}{}'.format(token, t, nonce)
string_to_sign = bytes(string_to_sign, 'utf-8')
sign = base64.b64encode(hmac.new(secret_key, msg=string_to_sign, digestmod=hashlib.sha256).digest())
return sign
def make_t():
t = int(round(time.time() * 1000))
return str(t)
def make_nonce():
nonce = str(uuid.uuid4())
return nonce
# load .env and get secret_key and token
load_dotenv()
secret_key = os.getenv("SECRET_KEY")
token = os.getenv("TOKEN")
# make secret_key, t, nonce, sign
secret_key = make_secret(secret_key)
t = make_t()
nonce = make_nonce()
sign = make_sign(secret_key, t, nonce)
# make headers
headers = {
"Authorization": token,
"sign": sign,
"t": t,
"nonce": nonce,
"Content-Type": "application/json; charset=utf-8"
}
# response
response = requests.get(URL, headers=headers)
pprint.pprint(response.json())