开发指南 计算 payload hash(无请求体时使用空字符串的SHA256) payloadhash hashlib.sha256(body.encode('utf8')).hexdigest() canonicalrequest ( f"{method.upper()}n" f"{canonicaluri}n" f"{canonicalquerystring}n" f"{canonicalheaders}n" f"{signedheadersstr}n" f"{payloadhash}" ) print("canonicalrequest:n", canonicalrequest) signature hmac.new( bytes(signingkey, 'utf8'), bytes(canonicalrequest, 'utf8'), hashlib.sha256 ).hexdigest() authorization f"{signingkeystr}/{signedheadersstr}/{signature}" return authorization def getcontentlength(self, data): body json.dumps(data) return str(len(body)) def getauth(self, url, method, canonicalquerystring, body''): canonicaluri self.generatecanonicaluri(url) canonicalheaders, signedheadersstr self.generatecanonicalheaders(headers) authorization self.generatesignature( XAPPID, XAPPKEY, REGION, TIMESTAMP, EXPIRATIONINSECONDS, method, canonicaluri, canonicalheaders, signedheadersstr, canonicalquerystring, body ) headers['Authorization'] authorization print(f"authorization {authorization}") return headers 使用示例 if name 'main': auth HttpAuth() resultheaders auth.getauth( url' method'POST', canonicalquerystring'', body'{"key":"value"}' ) print(resultheaders)