HTML 网页在线测试接口密钥是否有效。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>密钥测试</title>
<link rel="shortcut icon" href="https://github.githubassets.com/favicons/favicon.svg">
<style>
body {
font-family: Arial, sans-serif;
width: 95%;
margin: 0 auto;
}
textarea, input, button {
display: block;
margin: 15px 0;
padding: 10px;
width: 90%;
}
</style>
</head>
<body>
<textarea id="textarea" placeholder="输入问题"></textarea>
<input type="text" id="api_key" placeholder="输入API密钥">
<button id="openai-btn" data-api-url="https://api.openai.com/v1/chat/completions">测试官方密钥</button>
<button id="openkey-btn" data-api-url="https://openkey.cloud/v1/chat/completions">测试国内密钥</button>
<script>
let apiUrl = "";
document.getElementById("openai-btn").addEventListener("click", function() {
apiUrl = this.getAttribute("data-api-url");
testAPI();
});
document.getElementById("openkey-btn").addEventListener("click", function() {
apiUrl = this.getAttribute("data-api-url");
testAPI();
});
function testAPI() {
if (!apiUrl) {
alert("请选择一个API URL");
return;
}
const originalInput = document.getElementById('textarea').value;
document.getElementById('textarea').value = "请稍后...";
const apiKey = document.getElementById("api_key").value.trim();
const data = {
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: originalInput }
]
};
const xhr = new XMLHttpRequest();
xhr.open("POST", apiUrl, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", `Bearer ${apiKey}`);
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE) {
if (this.status === 200) {
const response = JSON.parse(this.responseText);
document.getElementById('textarea').value = response.choices[0].message.content;
} else {
document.getElementById('textarea').value = "请求失败,请检查您的API密钥和其他设置。";
}
}
};
xhr.send(JSON.stringify(data));
}
</script>
</body>
</html>