【发布时间】:2020-09-12 23:46:37
【问题描述】:
发起 HTTP 发布请求时出现错误:
'Access Denied: Invalid token, wrong code'。我已经尝试了所有可能的解决方案,但我无法通过此错误。
此挑战的详细信息:
授权
URL 受 HTTP 基本身份验证保护,RFC2617 第 2 章对此进行了说明,因此您必须在 POST 请求中提供 Authorization: 标头字段
对于 HTTP 基本身份验证的用户 ID,请使用您在 JSON 字符串中输入的相同电子邮件地址。 对于密码,请提供符合 RFC6238 TOTP 的 10 位基于时间的一次性密码。 授权密码 要生成 TOTP 密码,您需要使用以下设置:
您必须阅读 RFC6238(以及勘误表!)并自己获得正确的一次性密码。 TOTP 的 Time Step X 为 30 秒。 T0 为 0。 使用 HMAC-SHA-512 作为散列函数,而不是默认的 HMAC-SHA-1。 令牌共享密钥是用户标识,后跟 ASCII 字符串值“HENNGECHALLENGE003”(不包括双引号)。
const axios = require('axios');
const base64 = require('base-64');
const utf8 = require('utf8');
const { totp } = require('otplib');
const ReqJSON = {
"github_url": "ABC",
"contact_email": "ABC"
}
const stringData = JSON.stringify(ReqJSON);
const URL = "ABC";
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 {
const config = {
headers: {
'Content-Type': 'application/json',
"Authorization": "Basic " + encoded
}
};
console.log("Making request", {URL, ReqJSON, config});
const response = await axios.post(URL, stringData, config);
console.log(response.data);
} catch (err) {
console.error(err.response.data);
}
};
createReq();
【问题讨论】:
标签: node.js authentication http-headers totp