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

Amadeus API授权令牌获取失败:缺失grant_type参数求助

解决Amadeus API获取授权令牌时的Mandatory grant_type form parameter missing错误

针对你遇到的问题,给出以下几个排查和解决方向:

  • 先补全环境变量加载逻辑
    你的代码导入了dotenv但没有调用load_dotenv()方法,这会导致os.environ无法读取到.env文件中的密钥信息。建议在类初始化方法开头或者代码最顶部添加:

    load_dotenv()
    
  • 替换HTTPBasicAuth认证方式
    部分场景下,直接通过Base64编码凭证并放入请求头的方式比HTTPBasicAuth更稳定。修改get_new_token方法如下:

    import base64
    
    def get_new_token(self):
        # 拼接密钥并做Base64编码
        credentials = f"{self._api_key}:{self._api_secret}"
        encoded_cred = base64.b64encode(credentials.encode()).decode()
        
        # 构造完整请求头
        req_headers = self._header.copy()
        req_headers["Authorization"] = f"Basic {encoded_cred}"
        
        body = {'grant_type': 'client_credentials'}
        response = requests.post(
            url="https://test.api.amadeus.com/v1/security/oauth2/token",
            headers=req_headers,
            data=body
        )
        print(response.json())
    
  • 验证请求体传递格式
    尝试将请求体手动拼接为表单字符串,替代字典格式,确认是否是字典解析问题:

    body = "grant_type=client_credentials"
    
  • 确认密钥有效性
    在get_new_token方法开头打印self._api_key和self._api_secret,检查是否为空值或包含多余空格,确保复制的密钥完全正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 18:52:35