Created
August 27, 2019 05:27
-
-
Save lxneng/fb7f209a2d751658310d2cb7a4f9ba2f to your computer and use it in GitHub Desktop.
腾讯云API接口鉴权
This file contains hidden or 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
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