签名计算方式
签名生成通用步骤
1.
2.
3.
警告
1.
2.
3.
4.
5.
6.
sign签名加密算法
open_app_sign签名加密算法
举例
各语言请求示例
PHP请求示例
Python请求示例
import base64
import hashlib
import string
import time
from random import randint
from typing import Dict
import requests
# 随机字符串
def random_str(long=32):
base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
result = ""
for i in range(long):
result += base[randint(0, len(base) - 1)]
return result
# 封装md5
def md5(text: string):
hl = hashlib.md5()
hl.update(text.encode(encoding='utf-8'))
return hl.hexdigest()
# 字典排序
def k_sort_str(d: Dict[str, str]):
d_keys = list(d.keys())
d_keys.sort()
result = ""
for k in d_keys:
result += f"{k}={d[k]}"
if k != d_keys[-1]:
result += '&'
return result
# 演示请求
def demo_request():
# 定义当前时间戳
timestamp = str(int(time.time()))
# 人人商城V5店铺创建的API_KEY
api_key = 'V5suyZ45GcSyIURW2kPqVpQyQqC0Vx1z'
# 开放应用的APP_ID
open_app_id = '100001'
# 开放应用的APP_API_KEY
open_app_api_key = 'V9cfFexSl7ka79k7VM2L95XQNNxk7hAP'
# 请求接口地址
url = 'https://yousitedomain.com/wap/3/api/apps/openApi/user/index/check-password'
# 请求参数Dict
request_params = {
# 公共请求参数
'api_key': api_key,
'timestamp': timestamp,
'open_app_id': open_app_id,
'nonce_str': random_str(),
# 业务参数
'username': 'admin',
'password': 'admin',
}
# 字典排序后转为字符串
query_string = k_sort_str(request_params)
# 计算sign中的base64值
base64_str = str(timestamp) + api_key + query_string
base64_encode_str = base64.b64encode(base64_str.encode('utf-8')).decode('utf-8')
# 计算open_app_sign中的md5值
md5_encode = md5(timestamp + open_app_api_key + query_string)
# 计算签名
request_params.update({
'sign': md5(query_string + api_key + base64_encode_str + request_params['nonce_str']),
'open_app_sign': md5(
'913702023503242914' + open_app_id + query_string + md5_encode + request_params['nonce_str']),
})
# 执行请求
response = requests.request('POST', url, headers={}, data=request_params, files=[])
# 返回信息
print(response.text)
# 执行demo
demo_request()
Golang请求示例
Nodejs请求示例
Java请求示例
修改于 2024-08-22 09:11:10