Express post适用于请求模块,但不适用于axios

Express post适用于请求模块,但不适用于axios,express,request,axios,auth0,Express,Request,Axios,Auth0,在这件事上我浪费了两个小时的生命,想知道新鲜的眼睛是否能帮上忙 我正在尝试联系auth0以获取管理API的访问令牌 使用request模块提供示例代码,该模块工作正常(我已替换了key/secret值): 我的ID和密码存储在.env文件中,因此能够适应此情况,这也很好: var options = { method: 'POST', url: 'https://dev-wedegpdh.auth0.com/oauth/token', headers: { 'content-ty

在这件事上我浪费了两个小时的生命,想知道新鲜的眼睛是否能帮上忙

我正在尝试联系auth0以获取管理API的访问令牌

使用request模块提供示例代码,该模块工作正常(我已替换了key/secret值):

我的ID和密码存储在.env文件中,因此能够适应此情况,这也很好:

var options = { method: 'POST',
    url: 'https://dev-wedegpdh.auth0.com/oauth/token',
    headers: { 'content-type': 'application/json' },
    body: 
      JSON.stringify({
        grant_type: 'client_credentials',
        client_id: process.env.auth0AppKey,
        client_secret: process.env.auth0AppSecret,
        audience: 'https://dev-wedegpdh.auth0.com/api/v2/'})
  }

  request(options, function (error, response, body) {
    if (error) throw new Error(error)
    res.json(JSON.parse(response.body).access_token)
  })
我尝试使用axios发出完全相同的请求,但收到404错误:

let response = await axios.post(
  'https://dev-wedegpdh.auth0.com/api/v2/oauth/token', 
  JSON.stringify({
    grant_type: 'client_credentials',
    client_id: process.env.auth0AppKey,
    client_secret: process.env.auth0AppSecret,
    audience: 'https://dev-wedegpdh.auth0.com/api/v2/'
  }),
  { 
    headers: {'content-type': 'application/json'},
  }
)
我已经尝试了post函数的几种不同格式或配置,包括找到的那些 等等


有人看到我做错了什么吗?

在axios post body中,您需要以JSON格式发送数据,无需使用JSON.stringify

let response = await axios.post(
  "https://dev-wedegpdh.auth0.com/api/v2/oauth/token",
  {
    grant_type: "client_credentials",
    client_id: process.env.auth0AppKey,
    client_secret: process.env.auth0AppSecret,
    audience: "https://dev-wedegpdh.auth0.com/api/v2/"
  },
  {
    headers: { "content-type": "application/json" }
  }
);

在axios post正文中,您需要以JSON的形式发送数据,而无需使用JSON.stringify

let response = await axios.post(
  "https://dev-wedegpdh.auth0.com/api/v2/oauth/token",
  {
    grant_type: "client_credentials",
    client_id: process.env.auth0AppKey,
    client_secret: process.env.auth0AppSecret,
    audience: "https://dev-wedegpdh.auth0.com/api/v2/"
  },
  {
    headers: { "content-type": "application/json" }
  }
);