首页 新闻 会员 周边 捐助

星火知识库前端调用上传接口报错401 Signature cannot be verified

0
悬赏园豆:20 [待解决问题]

检查了appId、APISecret都没有问题
一直报解析签名出错,但是签名没有地方校验,我是哪里错了呢
// APPID: 'b7823b6a', // 控制台获取填写
// APISecret: 'NTU4ZTgwYTRhYzJjYTJmNzBmNmNlNDc1',
getHeader() {
let that = this
let timestamp = Math.floor(Date.now() / 1000).toString()
const headers = {
appId: that.APPID,
timestamp: timestamp,
signature: that.getSignature(that.APPID, that.APISecret, timestamp),
'content-type': 'multipart/form-data',
};
return headers;
},
getSignature(appId, secret, ts) {
const auth = md5(appId + ts);
const signature = CryptoJS.HmacSHA1(auth, secret).toString(CryptoJS.enc.Hex);
return signature
},
// 上传文档
uploadTxt() {
uni.chooseFile({
success: (chooseImageRes) => {
const tempFilePaths = chooseImageRes.tempFilePaths;
this.uploadDocument(tempFilePaths[0])
}
});
},
async uploadDocument(file) {
const formData = new FormData();
formData.append('file', file);
formData.append('fileName', '背影.txt');
formData.append('fileType', 'wiki');
const headers = this.getHeader();
try {
const response = await axios.post(
'https://chatdoc.xfyun.cn/openapi/v1/file/upload',
formData, {
headers
}
);
console.log('Response:', response.data);
} catch (error) {
console.error('Error:', error);
}
},

一只卧沙的雕的主页 一只卧沙的雕 | 初学一级 | 园豆:182
提问于:2024-09-10 17:20
< >
分享
所有回答(2)
0

低质量回答:

getHeader() {
let that = this;
let timestamp = Math.floor(Date.now() / 1000).toString();
const headers = {
appId: that.APPID,
timestamp: timestamp,
signature: that.getSignature(that.APPID, that.APISecret, timestamp),
'content-type': 'multipart/form-data',
};
return headers;
},

getSignature(appId, secret, ts) {
const auth = md5(appId + ts);
console.log('Auth:', auth); // 调试信息
const signature = CryptoJS.HmacSHA1(auth, secret).toString(CryptoJS.enc.Hex);
console.log('Signature:', signature); // 调试信息
return signature;
},

// 上传文档
uploadTxt() {
uni.chooseFile({
success: (chooseImageRes) => {
const tempFilePaths = chooseImageRes.tempFilePaths;
this.uploadDocument(tempFilePaths[0]);
}
});
},

async uploadDocument(file) {
const formData = new FormData();
formData.append('file', file);
formData.append('fileName', '背影.txt');
formData.append('fileType', 'wiki');
const headers = this.getHeader();
try {
const response = await axios.post(
'https://chatdoc.xfyun.cn/openapi/v1/file/upload',
formData, {
headers
}
);
console.log('Response:', response.data);
} catch (error) {
console.error('Error:', error);
}
}
CopyInsert

李宏飞 | 园豆:202 (菜鸟二级) | 2024-09-10 17:42

我没看出来哪里修改了呢,放进代码里还是报一样的错误

支持(0) 反对(0) 一只卧沙的雕 | 园豆:182 (初学一级) | 2024-09-10 17:49
0

那就主要检查这个方法,参考文档,看参数顺序和算法是否正确

getSignature(appId, secret, ts) {
  const auth = md5(appId + ts);
  const signature = CryptoJS.HmacSHA1(auth, secret).toString(CryptoJS.enc.Hex);
  return signature
}

talentzemin | 园豆:775 (小虾三级) | 2024-09-12 08:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册