m1/qcloud_sign.py
2020-04-17 11:37:13 +08:00

35 lines
1.0 KiB
Python

# -*- coding: utf8 -*-
# author:tan
# date:2018-12-10
# https://cloud.tencent.com/document/api/213/15693
"""
腾讯云api登陆鉴权
"""
import base64
import hashlib
import hmac
# 接口鉴权
class QcSign:
def __init__(self, secret_key, method, endpoint, params, v='3.0'):
self.method = method
self.endpoint = endpoint
self.sign_method = hashlib.sha1
self.secret_key = secret_key
self.params = params
self.version = v
# 生成签名串
def sign_str(self):
if self.version == '2.0':
s = self.method + self.endpoint + "?"
else:
s = self.method + self.endpoint + "/?"
query_str = "&".join("%s=%s" % (k, self.params[k]) for k in sorted(self.params))
# 拼接签名原文字符串 请求方法 + 请求主机 +请求路径 + ? + 请求字符串
string_to_sign = s + query_str
hmac_str = hmac.new(self.secret_key.encode("utf8"), string_to_sign.encode("utf8"), self.sign_method).digest()
return base64.b64encode(hmac_str)