请求失败,状态代码为 403,访问被拒绝:令牌无效,代码错误

时间:2021-04-12 08:53:35

标签: node.js axios

我正在尝试提交一个项目,但我无法提交,我已经检查了有关堆栈溢出的类似问题,但没有一个有解决方案。

我必须通过向 Basic AUTH 提供用户名和 TOTP 生成的密码来使用 POST 方法提交项目。每当我尝试提交它时,每次我都不确定我在这里做错了什么时都会遇到同样的问题。

这是我的代码

import { totp } from "otplib";
import axios from "axios";

const userId = "example@gmail.com";
const secret = "SECRETCODE";
const sharedSecret = userId + secret;

// JSON DATA
const reqBody = {
  {
  "github_url": "https://github.com/YOUR_ACCOUNT/GITHUB_REPOSITORY",
  "contact_email": "YOUR_EMAIL"
}
};

const stringfyJSON = JSON.stringify(reqBody);

totp.options = { digits: 10, algorithm: "sha512", step: 30 };

const token = totp.generate(sharedSecret);
const isValid = totp.check(token, sharedSecret);
const isVerified = totp.verify({ token, secret: sharedSecret });


const credentials = Buffer.from(
  `${reqBody.contact_email}:${token}`,
  "utf-8"
).toString("base64");
const auth = `Basic ${credentials}`;
console.log(auth);
console.log(credentials);

const createReq = async () => {
  try {
    const config = {
      withCredentials: true,
      headers: {
        "Content-Type": "application/json",
        Authorization: auth,
      },
    };
    console.log(
      "Making request to ",
      "REQURL"
    );

    await axios
      .post(
        "example@example.com",
        stringfyJSON,
        config
      )
      .then((res) => {
        console.log(res.data);
      });
  } catch (error) {
    console.log(error.message);
  }
};

createReq();

我已经做了所有的研究和一切,但似乎没有任何效果。

样品请求

POST /challenges/003 HTTP/1.1
Authorization: Basic bmluamFAZXhhbXBsZS5jb206MTU5NTk0MjU2MA==
Host: api.example.com
Accept: */*
Content-Type: application/json
Content-Length: 104

{"contact_email":"ninja@example.com", "github_url":"https://github.com/YOUR_ACCOUNT/GITHUB_REPOSITORY"

1 个答案:

答案 0 :(得分:0)

我曾经遇到过同样的问题,但我发现在使用标题时您需要像

一样将“=”放在末尾
Basic QXVYUVdTMHhMTmVCOXpKNjUzNTloOTVJZ0lDOndYaDNoNzlPNXdZbnJMVW5YNEo0QWFrZ2FveDY4akkyRVlQWGpWcUI0NWJtUnJuUW56dVNieWtVUDNYQ25pZzM=

所以在你的情况下

const auth = `Basic ${credentials}=`;
console.log(auth);