我正在尝试使用NODE
发出POST请求,但抛出错误。在这里,我想做的是在NODE中使用标头和SHA512哈希令牌生成TOTP。在标题中,我们必须传递我的私人GITHUB GIST URL和我的 EMAIL_ID ,这就是为什么我不能在这里共享它。您能帮忙吗?这是我的节点代码:
const axios = require('axios');
const base64 = require('base-64');
const utf8 = require('utf8');
const { totp } = require('otplib');
const reqJSON =
{
github_url: GITHUB_URL,
contact_email: MY_EMAIL
}
const stringData = JSON.stringify(reqJSON);
const URL = 'https://api.challenge.hennge.com/challenges/003';
const sharedSecret = reqJSON.contact_email + "HENNGECHALLENGE003";
totp.options = { digits: 10, algorithm: "sha512", epoch: 0 }
const myTotp = totp.generate(sharedSecret);
const isValid = totp.check(myTotp, sharedSecret);
console.log("Token Info:", {myTotp, isValid});
const authStringUTF = reqJSON.contact_email + ":" + myTotp;
const bytes = utf8.encode(authStringUTF);
const encoded = base64.encode(bytes);
const createReq = async () =>
{
try
{
// set the headers
const config = {
headers: {
'Content-Type': 'application/json',
"Authorization": "Basic " + encoded
}
};
console.log("Making req", {URL, reqJSON, config});
const res = await axios.post(URL, stringData, config);
console.log(res.data);
}
catch (err)
{
console.error(err.response.data);
}
};
createReq();
基本上,我要做的是提交我的私人gist github
个人资料中的代码。但是当我运行节点myfile.js
命令来运行时,它给了我错误:
Token Info: { myTotp: '1980479983', isValid: true }
Making req {
URL: 'https://api.challenge.hennge.com/challenges/003',
reqJSON: {
github_url: 'https://gist.github.com/sthakur369/233ab97e2d4c4a67630bf6c61f629fe1',
contact_email: 'sthakur369@gmail.com'
},
config: {
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic c3RoYWt1cjM2OUBnbWFpbC5jb206MTk4MDQ3OTk4Mw=='
}
}
}
{ message: 'Access denied: Invalid token, wrong code' }
提前谢谢!!我要做的就是提交代码,使我完全陷入困境。