You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用axios调用API创建密钥时出现401状态码错误求助

排查401授权失败的实用思路

核心排查步骤

  • 校验JWT令牌有效性
    直接解码拿到的token,重点检查:

    • exp字段是否已过期
    • iss/aud等声明是否与目标接口要求匹配
    • 确认token未被篡改,签名合法
  • 检查请求头格式
    确保Authorization头严格遵循Bearer [空格]token的格式,无多余空格或换行。可以在代码中添加日志输出headers.Authorization,验证实际发送的内容是否正确。

  • 移除不必要的跨域配置
    若目标API不需要携带跨域Cookie,删除withCredentials: true。JWT认证场景下该配置大多多余,反而可能导致请求头异常。

  • 核对接口参数
    确认请求URLhttps://sandbox.fluidpay.com/api/user/apikey及POST方法完全符合API文档要求,部分密钥创建接口可能使用PUT或不同路径。

  • 检查令牌权限范围
    获取JWT的接口可能仅返回基础权限令牌,而创建API密钥需要更高权限。查看文档确认获取JWT时是否需指定特定scope或权限参数。

报错信息

error: "Request failed with status code 401"

你的代码

const createFpAPIKey = async (token) => {
    const raw = { name: "any name", type: "api" };
    const headers = {
          "Content-Type": "application/json",
          Authorization: `Bearer ${token}`,
          };
    const url = "https://sandbox.fluidpay.com/api/user/apikey";
    let r = {};
    try {
      r = await axios({
        method: "post",
        url,
        data: raw,
        headers,
        withCredentials: true,
      });
      r = r.data;
      if (typeof r.body === "string") r = JSON.parse(r.body);
    } catch (e) {
      r = {
        errorType: "Error Fetching API",
        error: e.message,
      };
    }
    return r;
  },

内容的提问来源于stack exchange,提问作者Mustafa Yusuf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 01:35:28