Created
October 9, 2022 00:34
-
-
Save iCarrot0605/472dd85f887985a67b9f956070cf9864 to your computer and use it in GitHub Desktop.
Generate Switchbot API v1.1 sign header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# coding: utf-8 | |
''' Create Switchbot sign header ''' | |
import time | |
import hashlib | |
import hmac | |
import base64 | |
import json | |
import uuid | |
def gensign(token, secret): | |
''' | |
Generate Switchbot API v1.1 sign header | |
Args: | |
token: string :copy and paste from the SwitchBot app V6.14 or later | |
secret: string :copy and paste from the SwitchBot app V6.14 or later | |
Returns: | |
Switchbot API v1.1 sign header | |
''' | |
nonce = str(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()) | |
header={} | |
header["Authorization"] = token | |
header["sign"] = str(sign, 'utf-8') | |
header["t"] = str(t) | |
header["nonce"] = nonce | |
return header |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment