大家有使用api进行站点缓存清理的吗 我使用python3 清理缓存一直报Bad signature:sign error! sign or key NG:X-Auth-Access-Key 错误
答:签名错误
api_url = ‘https://api.su.baidu.com/%s’
# X-Auth-Access-Key
access_key = ”
# X-Auth-Signature-Method
secret_key = ”
# X-Auth-Nonce
ran_str = str(”.join(random.sample(string.ascii_letters + string.digits, 32)))
def build_sign(secret_key, params):
”’通过HMAC-SHA1构造签名串”’
”’
Args:
secret_key: HMAC – SHA1所使用的key
params: 待签名的参数dict
”’
try:
ks = list(params.keys())
ks.sort()
base_str = ‘&’.join([‘%s=%s’ % (k, str(params[k])) for k in ks])
return base64.b64encode(hmac.new(bytes(secret_key, ‘utf-8’), bytes(base_str, ‘utf-8’), hashlib.sha1).digest())
except Exception as e:
import traceback
print(traceback.format_exc())
return e
def build_headers(access_key, secret_key, path, get_params={}, post_params={}):
“””
根据请求参数构建包含鉴权参数的请求Header
Args:
access_key: ak
secret_key: sk
get_params: 业务相关的GET参数
post_params: 业务相关的POST参数
view_params: url中的restful参数
Returns:
params: 添加了鉴权相关参数,并且签名过的参数
“””
headers = {‘X-Auth-Access-Key’: access_key, ‘X-Auth-Timestamp’: str(int(time.time())),
‘X-Auth-Signature-Method’: ‘HMAC-SHA1’, ‘X-Auth-Nonce’: ran_str}
all_params = {}
all_params.update(get_params)
all_params.update(post_params)
all_params.update(headers)
all_params[‘X-Auth-Path-Info’] = path.strip(‘/’)
auth_sign = build_sign(secret_key, all_params)
headers[‘X-Auth-Sign’] = auth_sign
return headers
ak 和 sk 填上去
以上答案来自于百度云加速交流群@柠檬不萌有点酸同学回答。