Skip to content

Instantly share code, notes, and snippets.

@lxneng
Created August 27, 2019 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lxneng/fb7f209a2d751658310d2cb7a4f9ba2f to your computer and use it in GitHub Desktop.
Save lxneng/fb7f209a2d751658310d2cb7a4f9ba2f to your computer and use it in GitHub Desktop.
腾讯云API接口鉴权
import hmac
import base64
def make_qcloud_signature(req_params: dict, req_host: str, req_path: str,
req_method: str, secret_key: str) -> str:
""" Make QCloud API Signature
https://cloud.tencent.com/document/product/271/2053
params example:
```
req_params = {}
req_host = 'wenzhi.api.qcloud.com'
req_method = 'POST'
req_path = '/v2/index.php'
secret_key = 'sdfsdfsdfsdfsd'
```
Returns: Signature string
"""
sort_dict = sorted(zip(params.keys(), params.values()))
req_query = '&'.join([f"{k}={v}" for k, v in sort_dict])
s1 = f"{req_method}{req_host}{req_path}?{req_query}"
s2 = hmac.new(secret_key.encode(), s1.encode(), hashlib.sha1).digest()
return base64.b64encode(s2).decode()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment