如何解决缺少关键字参数明确包含在 api 调用中
我正在尝试设置对 Amadeus 的基本 API 调用。所有正确的参数都包含在内,我正在为每次调用获取一个新令牌。
这是一个 React/Flask 应用程序。
这是我在 app/routes.py 中的端点:
@app.route('/search',methods=['GET'])
@cross_origin(origin='*')
def get_flights():
api_key = os.environ.get('amadeus_api_key',None)
api_secret = os.environ.get('amadeus_api_secret',None)
# first,you must get an access token using your Amadeus credentials
token_request = requests.post(
'https://test.api.amadeus.com/v1/security/oauth2/token',data = {
'grant_type': 'client_credentials','client_id': api_key,'client_secret': api_secret
}
)
token = token_request.json()['access_token']
bearer = 'Bearer {}'.format(token)
locations = requests.get(
'https://test.api.amadeus.com/v1/reference-data/locations',headers = {
'Authorization': bearer
},data = {
'subType': 'AIRPORT','keyword': 'BOS'
}
)
print(locations.json())
# example:
# https://test.api.amadeus.com/v1/reference-data/locations
# ?subType=AIRPORT&keyword=BOS
return jsonify({'token': token})
错误如下:
{'errors': [{'status': 400,'code': 32171,'title': 'MANDATORY DATA MISSING','detail': 'Missing mandatory query parameter','source': {'parameter': 'keyword'}}]}
正如您在 /search 端点中看到的,关键字参数已明确包含在内。
什么给?我错过了什么吗?
解决方法
获取位置的请求未正确构建,因为它将 subType
和 keyword
作为正文而不是查询参数发送。根据 requests
文档,您需要使用 params
:
locations = requests.get(
'https://test.api.amadeus.com/v1/reference-data/locations',headers = {
'Authorization': bearer
},params = {
'subType': 'AIRPORT','keyword': 'BOS'
}
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。