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

Amadeus API授权请求提示缺少grant_type参数的axios调用问题

Amadeus授权token请求报错修复

错误返回

接口返回的错误信息如下:

code: 38187
error: "invalid_request"
error_description: "Mandatory grant_type form parameter missing"
title: "Invalid parameters"

差异定位

和可正常运行的curl请求对比,代码存在两处问题:

  • 初始请求头Content-Type配置错误,缺失application/前缀,正确值应为application/x-www-form-urlencoded
  • 核心错误:即使修正请求头,传入data字段的是JavaScript普通对象,axios默认会将普通对象序列化为JSON格式发送,不会自动转换为application/x-www-form-urlencoded要求的key=value&key2=value2表单字符串格式,服务端解析请求体时无法读取到grant_type等表单参数,因此持续报参数缺失错误。

修复代码

使用原生URLSearchParams构造表单格式数据即可,无需额外依赖,兼容现代浏览器和Node.js 10+版本:

async getAmadeusKey() {
    const response = await axios({
        url: "https://test.api.amadeus.com/v1/security/oauth2/token",
        method: 'post',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: new URLSearchParams({
            grant_type: 'client_credentials',
            client_id: CLIENT_ID,
            client_secret: CLIENT_SECRET,
        })
    });
    console.log(response.data);
}

如果是低版本Node环境,也可以手动拼接编码后的表单字符串:

async getAmadeusKey() {
    const formData = [
        `grant_type=client_credentials`,
        `client_id=${encodeURIComponent(CLIENT_ID)}`,
        `client_secret=${encodeURIComponent(CLIENT_SECRET)}`
    ].join('&')
    const response = await axios({
        url: "https://test.api.amadeus.com/v1/security/oauth2/token",
        method: 'post',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: formData
    });
    console.log(response.data);
}

校验方法

可以通过axios请求拦截器打印最终发出的请求体,正常格式应与curl中-d参数后的内容完全一致,为grant_type=client_credentials&client_id=xxx&client_secret=xxx形式,而非JSON字符串,即代表请求构造正确。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 03:09:19