前言:

三方平台已经封装好的API,现在需简单测试验证API是否ok;
涉及请求前的鉴权签名 与 对应业务的返回是否OK。
1.鉴权签名

1.1 使用md5在线加密:
获取appsecret
获取appkey
获取timestamp:Date.now(),1739868388963然后改为1839868388963

在线md5加密

1.2 代码加密
首先通过页面输入appkey、appsecret与当前时间戳,通过md5库,计算得到鉴权参数
然后,返回鉴权参数;
然后测试业务接口:根据业务接口与参数要求,组装请求的url。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API签名计算器</title>
<!-- 添加 MD5 库 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-md5/2.19.0/js/md5.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 20px;
}
.input-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
}
input {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
#result {
margin-top: 20px;
padding: 15px;
background-color: #f5f5f5;
border-radius: 4px;
}
.code {
font-family: monospace;
background-color: #e8e8e8;
padding: 2px 4px;
border-radius: 3px;
}
/* 添加新样式 */
.api-test {
margin-top: 30px;
padding-top: 20px;
border-top: 2px solid #ddd;
}
select {
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 10px;
}
textarea {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 4px;
min-height: 100px;
font-family: monospace;
}
.response {
margin-top: 20px;
padding: 15px;
background-color: #f5f5f5;
border-radius: 4px;
overflow-x: auto;
}
.error {
color: #d32f2f;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>酷家乐API测试工具</h1>
<!-- 鉴权参数部分 -->
<div class="section">
<h2>鉴权参数</h2>
<div class="input-group">
<label for="appSecret">App Secret:</label>
<input type="text" id="appSecret" placeholder="输入 App Secret">
</div>
<div class="input-group">
<label for="appKey">App Key:</label>
<input type="text" id="appKey" placeholder="输入 App Key">
</div>
<div class="input-group">
<label for="appuid">App UID (可选):</label>
<input type="text" id="appuid" placeholder="输入 App UID(可选)">
</div>
</div>
<!-- API测试部分 -->
<div class="api-test">
<h2>API测试</h2>
<div class="input-group">
<label for="method">请求方法:</label>
<select id="method">
<option value="GET">GET</option>
<option value="POST">POST</option>
<option value="PUT">PUT</option>
<option value="DELETE">DELETE</option>
</select>
</div>
<div class="input-group">
<label for="apiUrl">请求地址:</label>
<input type="text" id="apiUrl" placeholder="输入完整的API地址">
</div>
<div class="input-group">
<label for="requestBody">请求参数 (JSON格式):</label>
<textarea id="requestBody" placeholder='{"key": "value"}'></textarea>
</div>
<button onclick="sendRequest()">发送请求</button>
<div id="apiResponse" class="response"></div>
</div>
<script>
function calculateSign() {
const appSecret = document.getElementById('appSecret').value.trim();
const appKey = document.getElementById('appKey').value.trim();
const appuid = document.getElementById('appuid').value.trim();
const timestamp = '1839866826719'// Date.now();
if (!appSecret || !appKey) {
alert('请输入 App Secret 和 App Key');
return;
}
// 根据是否存在appuid使用不同的签名计算方式
let signStr;
if (appuid) {
// 如果appuid存在:sign = md5(appsecret + appkey + appuid + timestamp)
signStr = appSecret + appKey + appuid + timestamp;
} else {
// 如果appuid不存在:sign = md5(appsecret + appkey + timestamp)
signStr = appSecret + appKey + timestamp;
}
// 计算 MD5
const sign = md5(signStr);
// 显示结果
document.getElementById('result').innerHTML = `
<h3>计算结果:</h3>
<p><strong>时间戳:</strong> <span class="code">${timestamp}</span></p>
<p><strong>签名字符串:</strong> <span class="code">${signStr}</span></p>
<p><strong>MD5签名:</strong> <span class="code">${sign}</span></p>
<p><strong>完整参数:</strong></p>
<pre>${JSON.stringify({
app_key: appKey,
timestamp: timestamp,
...(appuid ? { appuid: appuid } : {}),
sign: sign
}, null, 2)}</pre>
`;
}
</script>
</body>
</html>
2.请求测试

测试API:

2.1使用postman在线测试

2.2 使用express静态托管html服务
(1) 页面

项目目录:

(2) index.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>酷家乐API测试工具</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-md5/2.19.0/js/md5.min.js"></script>
<style>
:root {
--primary-color: #FF6C37;
--secondary-color: #F4F4F4;
--border-color: #E8E8E8;
--text-color: #333333;
--header-bg: #FFFFFF;
--sidebar-bg: #FAFAFA;
--content-bg: #FFFFFF;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
color: var(--text-color);
background-color: var(--secondary-color);
line-height: 1.6;
}
.container {
display: grid;
grid-template-columns: 300px 1fr;
min-height: 100vh;
gap: 20px;
padding: 20px;
background-color: var(--secondary-color);
}
/* 左侧边栏样式 */
.sidebar {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
height: calc(100vh - 40px);
position: sticky;
top: 20px;
}
.sidebar h2 {
margin-bottom: 20px;
color: var(--text-color);
}
/* 右侧主内容区样式 */
.main-content {
display: flex;
flex-direction: column;
gap: 20px;
}
/* 鉴权面板样式 */
.auth-panel {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
.auth-panel h2 {
margin-bottom: 20px;
color: var(--text-color);
}
.auth-form {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin-bottom: 20px;
}
/* 请求面板样式 */
.request-panel {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
/* 历史记录列表样式优化 */
.history-list {
height: calc(100% - 60px);
overflow-y: auto;
border: 1px solid var(--border-color);
border-radius: 4px;
}

最低0.47元/天 解锁文章
3127

被折叠的 条评论
为什么被折叠?



