物联网平台中调用云端api,python的签名实现。
import time import sys import hashlib import hmac import base64 import urllib.parse def do_sign(secret, sign_content): m = hmac.new(secret, sign_content, digestmod=hashlib.sha1) return base64.b64encode(m.digest()).decode("utf-8") def encode(str): result = urllib.parse.quote(str); print ("encode===="+result) return result accessKeyId = ""; accessSecret = ""; httpmethod = "POST" parm = { "Format":"JSON", "Version":"2018-01-20", "AccessKeyId":accessKeyId, "SignatureMethod":"HMAC-SHA1", "Timestamp":"2021-12-28T06:37:48Z", "SignatureVersion":"1.0", "SignatureNonce":"86439216304009", "Action":"QueryDevice", "ProductKey":"a1tDKl1PBkl", "RegionId":"cn-shanghai" } str_parm = '' for p in sorted(parm): if(len(str_parm)>0): str_parm = str_parm + '&' if isinstance(parm[p],str): str_parm = str_parm + str(p) +"="+str(encode(parm[p])) continue str_parm = str_parm + str(p) +"="+encode(str(parm[p])) str_parm = httpmethod + '&' + "%2F" + '&' + encode(str_parm); print (str_parm) key = accessSecret+'&'; signature = do_sign(key.encode("utf-8"), str_parm.encode("utf-8")) print ("signature==="+signature) print ("最终signature===" + encode(signature))
js的签名过程参考:https://developer.aliyun.com/article/846040?spm=a2c6h.13148508.0.0.7bff4f0eveyj4Z