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
相关产品推荐
相关产品推荐

