121 lines
4.1 KiB
JavaScript
121 lines
4.1 KiB
JavaScript
/**
|
|
* @file test_volc_ark_apikey.js
|
|
* @description 测试火山方舟 API Key 访问连通性(对应 CSDN 文章第五步:验证 API 连通性)
|
|
* @author 姜维 - 平台总督
|
|
* @date 2026-03-31
|
|
*/
|
|
|
|
const https = require('https');
|
|
const http = require('http');
|
|
|
|
// 从配置中读取(这里使用配置中的信息)
|
|
const VOLC_CONFIG = {
|
|
endpoint: 'https://ark.cn-beijing.volces.com/api/v3',
|
|
// 注意:实际运行时,请确保环境变量中已配置正确的 API Key
|
|
// 这里只做连通性测试
|
|
};
|
|
|
|
function testHttpsConnection() {
|
|
console.log('='.repeat(60));
|
|
console.log('🧪 开始测试火山方舟 API 连通性');
|
|
console.log('📌 目标端点: ' + VOLC_CONFIG.endpoint);
|
|
console.log('='.repeat(60));
|
|
console.log('');
|
|
|
|
const url = new URL(VOLC_CONFIG.endpoint + '/chat/completions');
|
|
|
|
const options = {
|
|
hostname: url.hostname,
|
|
port: url.port || (url.protocol === 'https:' ? 443 : 80),
|
|
path: url.pathname,
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
// 如果有 API Key,会在这里传递
|
|
}
|
|
};
|
|
|
|
console.log('🔗 正在建立 HTTPS 连接...');
|
|
console.log(`📍 Host: ${options.hostname}`);
|
|
console.log(`📍 Port: ${options.port}`);
|
|
console.log(`📍 Path: ${options.path}`);
|
|
console.log('');
|
|
|
|
const requester = url.protocol === 'https:' ? https : http;
|
|
|
|
const req = requester.request(options, (res) => {
|
|
console.log(`✅ 连接已建立,状态码: ${res.statusCode}`);
|
|
console.log(`📋 响应头:`);
|
|
Object.entries(res.headers).forEach(([key, value]) => {
|
|
console.log(` ${key}: ${value}`);
|
|
});
|
|
console.log('');
|
|
|
|
let data = '';
|
|
res.on('data', (chunk) => {
|
|
data += chunk;
|
|
});
|
|
|
|
res.on('end', () => {
|
|
console.log('📄 响应内容:');
|
|
console.log('-' .repeat(60));
|
|
try {
|
|
const parsed = JSON.parse(data);
|
|
console.log(JSON.stringify(parsed, null, 2));
|
|
} catch (e) {
|
|
console.log(data);
|
|
}
|
|
console.log('-' .repeat(60));
|
|
console.log('');
|
|
|
|
if (res.statusCode === 401) {
|
|
console.log('🔍 结果分析:');
|
|
console.log('✅ HTTPS 连接成功!SSL 证书验证通过');
|
|
console.log('ℹ️ 401 是正常的,因为我们没传正确的 API Key');
|
|
console.log('✅ 结论:SSL/TLS 连接正常,没有证书问题');
|
|
} else if (res.statusCode === 200) {
|
|
console.log('✅ 连接成功,认证成功');
|
|
} else {
|
|
console.log('⚠️ 连接建立成功,但返回了非预期状态码');
|
|
}
|
|
console.log('');
|
|
});
|
|
});
|
|
|
|
req.on('error', (e) => {
|
|
console.log('❌ 连接失败:');
|
|
console.log(` 错误: ${e.message}`);
|
|
console.log('');
|
|
console.log('🔍 可能原因分析:');
|
|
if (e.message.includes('SSL')) {
|
|
console.log(' 📛 SSL 证书验证失败 → 这就是 CSDN 文章说的问题');
|
|
console.log(' 💡 解决方案: 设置 NODE_TLS_REJECT_UNAUTHORIZED=0');
|
|
} else if (e.message.includes('ECONNREFUSED')) {
|
|
console.log(' 📛 连接被拒绝 → 服务没启动或者端口错了');
|
|
} else if (e.message.includes('ETIMEDOUT')) {
|
|
console.log(' 📛 连接超时 → 网络不通或者防火墙拦截');
|
|
} else if (e.message.includes('getaddrinfo')) {
|
|
console.log(' 📛 DNS 解析失败 → 域名错了');
|
|
}
|
|
console.log('');
|
|
});
|
|
|
|
// 发送一个空请求,只测试连通性
|
|
const testBody = {
|
|
model: 'doubao-seed-2.0-lite',
|
|
messages: [
|
|
{ role: 'user', content: 'Hello' }
|
|
]
|
|
};
|
|
|
|
req.write(JSON.stringify(testBody));
|
|
req.end();
|
|
}
|
|
|
|
// 如果直接运行,则执行测试
|
|
if (require.main === module) {
|
|
testHttpsConnection();
|
|
}
|
|
|
|
module.exports = { testHttpsConnection };
|