使用Amadeus JavaScript入门指南遇ClientError及无效令牌问题求助
排查Amadeus API认证问题(ClientError及无效token)
一、SDK调用报错(ClientError)排查
- 检查API密钥格式:你的代码中
clientId和clientSecret使用了{API key}和{API secret}的占位符格式,替换时需完全移除大括号,直接填入实际密钥字符串,示例:const amadeus = new Amadeus({ clientId: '你的实际API Key', clientSecret: '你的实际API Secret' }); - 打印完整错误信息:当前catch块仅打印
responseError.code,修改为打印完整错误对象,可获取更详细的失败原因:.catch(function(responseError){ console.log('完整错误信息:', responseError); }); - 确认SDK自动token机制:Amadeus Node.js SDK会自动处理token的获取与刷新,若网络存在代理或防火墙限制,需添加代理配置:
const amadeus = new Amadeus({ clientId: '你的API Key', clientSecret: '你的API Secret', proxy: 'http://your-proxy-url:port' });
二、Curl调用报错(无效access token)排查
- 验证token完整性:获取token后,替换
{{token}}时需移除大括号,且确保token字符串无多余空格、换行符。正确的请求头示例:-H 'Authorization: Bearer 实际的token字符串' - 确认环境一致性:获取token和调用接口必须使用同一环境(均为
test.api.amadeus.com),禁止混用测试与生产环境的密钥或域名。 - 检查token有效期:Amadeus access token有效期为30分钟,若获取后间隔过久调用,需重新生成新token。
- 核对请求头拼写:确保
Bearer首字母大写,且与token之间有一个空格,避免拼写错误导致认证失败。
三、额外优化建议
将API调用逻辑放到Express路由中,而非直接在启动时执行,避免异步时序问题引发异常:
app.get('/flight-offers', async (req, res) => { try { const response = await amadeus.shopping.flightOffersSearch.get({ originLocationCode: 'SYD', destinationLocationCode: 'BKK', departureDate: '2022-06-01', adults: '2' }); res.json(response.data); } catch (error) { res.status(error.response.status).json(error.response.data); } });
内容的提问来源于stack exchange,提问作者A Brooks2000
相关产品推荐
相关产品推荐

