获取令牌的请求未传递到下一个获取请求,返回 401

Fetch requests for token not passed to next fetch request, 401 returned

我正在使用 fetch 连接到端点以获取不记名令牌,然后将其用于另一个 fetch 请求以进行身份​​验证。我首先使用了 Postman 并验证了我正在访问的端点是否正常工作,但是根据下面的获取请求,我得到了 401。

我已经在控制台中检查了授权在第一次获取后更新并传递给了第二次但是我继续得到 401。

第一次调用到达./auth 并返回不记名令牌。该令牌传递给下一个获取请求,但我收到 401。

我遗漏了什么或做错了什么,第二双眼睛可以提供帮助。

const token = "./auth";
const listings = "./listings";
let clientHeaders = new Headers();
let raw = JSON.stringify({
  email: "fake@account.com",
  password: "12345",
});

clientHeaders.append("Authorization", "");
clientHeaders.append("Content-Type", "application/json");
clientHeaders.append("Cookie", "");

let req = {
  method: "POST",
  headers: clientHeaders,
  body: raw,
  redirect: "follow",
};

fetch(token, req)
  .then((response) => response.json())

  .then((result) => {
    return fetch(listings, {
      method: "GET",
      headers: {
        Authorization: result.token,
        "Content-Type": "application/json",
      },
    });
  })
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    console.log(JSON.parse(data));
  })
  .catch((error) => {
    console.log("error: ", error);
  });

您可能 need to include 字面单词“Bearer”和 header 中标记前的 space:

{
  Authorization: `Bearer ${result.token}`
}

// or

{
  Authorization: "Bearer " + result.token
}