编辑代码

# coding:utf-8
from urllib import parse
import base64
import hashlib
import hmac

def hmacSha256(key, message):
    key = bytes(key, 'utf-8')
    message = bytes(message, 'utf-8')
    hmacObject = hmac.new(key, message, hashlib.sha256)
    return hmacObject.hexdigest()

def sha256hex(message):
    sha256Object = hashlib.sha256()
    message = bytes(message, 'utf-8')
    sha256Object.update(message)
    return sha256Object.hexdigest()

# 以下信息为假设,请按照实际情况修改(仅为示例,并非实际数据)
appID = 'eVzjIKe1NZe9Tfpb'
appSecret = 'nzWChGoCWnAwSxEsMRXFDKwDeKIfZhVbjeyzZooBnnJa5KBQC4Zd1lmYVy8AEanM'
method = 'GET'
requestPath = '/api/zhaozhao/涂卡'
timeStamp = '1746689979576'

# 拼接请求信息,请求路径如有中文必须进行 url 编码,编码必须为小写
requestStr = "Findluo" + "\n" + method + "\n" + parse.quote(requestPath).lower()

# 生成签名字符串
signHexStr = sha256hex(requestStr)
signStr = base64.b64encode(bytes(signHexStr + timeStamp, 'utf-8')).decode()

# 计算最终签名
signKey = hmacSha256(appSecret, signStr)

# 拼接凭证字符串
authContent = "Findluo" + "\n" + appID + "\n" + signKey + "\n" + timeStamp

# 拼接 Authorization 请求值
authHeaderValue = "Findluo " + base64.b64encode(bytes(authContent, 'utf-8')).decode()

print('url: ' + requestPath)
print('SHA256 请求信息签名字符串: ' + signHexStr)
print('签名字符串: ' + signStr)
print('最终签名: ' + signKey)
print('Authorization: ' + authHeaderValue)