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

Python调用Amadeus API获取Access Token失败求助

解决Amadeus Flight API认证获取Access Token失败问题

问题根源分析

你遇到的No access_token parameter needed for this method错误,本质是请求参数格式或参数名不符合Amadeus OAuth2认证要求:

  • Requests库代码问题:设置了Content-Type: application/x-www-form-urlencoded,却用json参数传递数据(实际发送JSON格式请求体),且参数名client id拼写错误(应为client_id)。
  • requests-oauthlib库问题:默认请求逻辑可能附带不必要参数,或请求格式未匹配Amadeus的要求。

修正后的Requests库代码

import requests

API_KEY = "twAtndd5V..."
API_SECRET = "xtj..."
AUTH_ENDPOINT = "https://test.api.amadeus.com/v1/security/oauth2/token"

# 使用data参数传递表单格式数据,修正参数名
response = requests.post(
    url=AUTH_ENDPOINT,
    data={
        "grant_type": "client_credentials",
        "client_id": API_KEY,
        "client_secret": API_SECRET
    },
    headers={"Content-Type": "application/x-www-form-urlencoded"}
)
print(response.text)

修正后的requests-oauthlib库代码

通过auth参数使用HTTP Basic认证(Amadeus官方推荐方式),避免参数格式冲突:

from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient

API_KEY = "twAtndd5V..."
API_SECRET = "xtj..."
AUTH_ENDPOINT = "https://test.api.amadeus.com/v1/security/oauth2/token"

client = BackendApplicationClient(client_id=API_KEY)
oauth = OAuth2Session(client=client)
# 用HTTP Basic认证自动处理client_id和secret的编码
token = oauth.fetch_token(
    token_url=AUTH_ENDPOINT,
    auth=(API_KEY, API_SECRET)
)
print(token)

额外注意事项

  • 确保AUTH_ENDPOINT末尾无多余斜杠(部分API对路径格式敏感)
  • 检查API_KEY和API_SECRET是否复制正确,避免含多余空格或字符

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 05:17:23