如何从Airbnb API获取JSON响应?遇400无效密钥错误求助
解决Airbnb API调用返回"Invalid API key"的400错误
问题描述
尝试用Python调用Airbnb的StaysPdpSections接口获取房源数据,但返回400错误,错误响应显示:
{ "errors": [ { "message": "Response not successful: Received status code 400", "locations": [{"line": 1,"column": 1}], "path": [], "extensions": { "response": { "statusCode": 400, "body": { "error_code": 400, "error": "invalid_key", "error_message": "Invalid API key." } }, "code": "Bad Request" } } ], "data": null }
调用代码如下:
import json from urllib.request import urlopen def fetch_airbnb_data(): api_url = "https://www.airbnb.co.uk/api/v3/StaysPdpSections/3bcbe9ee20da9daa6722934f0b54ef68db41699d179e698c3aeab4ec30a964cb?operationName=StaysPdpSections&locale=en-GB&currency=GBP&variables=%7B%22id%22%3A%22U3RheUxpc3Rpbmc6MTAyNzQ1NzkyNDI2OTA2NDk4OQ%3D%3D%22%2C%22pdpSectionsRequest%22%3A%7B%22adults%22%3A%221%22%2C%22amenityFilters%22%3Anull%2C%22bypassTargetings%22%3Afalse%2C%22categoryTag%22%3Anull%2C%22children%22%3Anull%2C%22disasterId%22%3Anull%2C%22discountedGuestFeeVersion%22%3Anull%2C%22displayExtensions%22%3Anull%2C%22federatedSearchId%22%3Anull%2C%22forceBoostPriorityMessageType%22%3Anull%2C%22hostPreview%22%3Afalse%2C%22infants%22%3Anull%2C%22interactionType%22%3Anull%2C%22layouts%22%3A%5B%22SIDEBAR%22%2C%22SINGLE_COLUMN%22%5D%2C%22pets%22%3A0%2C%22pdpTypeOverride%22%3Anull%2C%22photoId%22%3Anull%2C%22preview%22%3Afalse%2C%22previousStateCheckIn%22%3Anull%2C%22previousStateCheckOut%22%3Anull%2C%22priceDropSource%22%3Anull%2C%22privateBooking%22%3Afalse%2C%22promotionUuid%22%3Anull%2C%22relaxedAmenityIds%22%3Anull%2C%22searchId%22%3Anull%2C%22selectedCancellationPolicyId%22%3Anull%2C%22selectedRatePlanId%22%3Anull%2C%22splitStays%22%3Anull%2C%22staysBookingMigrationEnabled%22%3Afalse%2C%22translateUgc%22%3Anull%2C%22useNewSectionWrapperApi%22%3Afalse%2C%22sectionIds%22%3Anull%2C%22checkIn%22%3Anull%2C%22checkOut%22%3Anull%2C%22p3ImpressionId%22%3A%22p3_1729488926_P3eUXvpmVbFyX_Cq%22%7D%7D&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%223bcbe9ee20da9daa6722934f0b54ef68db41699d179e698c3aeab4ec30a964cb%22%7D%7D" try: jsonurl = urlopen(api_url) data = json.loads(jsonurl.read()) print(json.dumps(data, indent=4)) except Exception as e: print(f"Error fetching data: {e}") fetch_airbnb_data()
问题原因
你调用的是Airbnb网页端使用的私有GraphQL接口,这类接口需要携带合法的验证信息(如API密钥、Cookie、请求头等)才能访问。直接裸请求没有任何验证标识,会被服务器判定为无效请求,返回"invalid_key"错误。
解决方法
1. 改用官方公开API(推荐商业场景)
如果是商业用途,需申请Airbnb官方的Partner API,获取合法的API密钥后,按照官方文档要求构造请求。通常需要在请求头中添加授权字段,例如:
headers = { 'Authorization': 'Bearer YOUR_OFFICIAL_API_KEY', 'Content-Type': 'application/json' }
2. 模拟浏览器请求(仅用于个人学习测试)
如果只是个人学习,可通过模拟浏览器的请求头绕过基础验证。至少需要添加User-Agent字段模拟真实浏览器,若仍失败,可从浏览器开发者工具中复制网页请求的Cookie、X-Airbnb-API-Key等字段补充到请求头:
import json import urllib.request def fetch_airbnb_data(): api_url = "https://www.airbnb.co.uk/api/v3/StaysPdpSections/3bcbe9ee20da9daa6722934f0b54ef68db41699d179e698c3aeab4ec30a964cb?operationName=StaysPdpSections&locale=en-GB¤cy=GBP&variables=%7B%22id%22%3A%22U3RheUxpc3Rpbmc6MTAyNzQ1NzkyNDI2OTA2NDk4OQ%3D%3D%22%2C%22pdpSectionsRequest%22%3A%7B%22adults%22%3A%221%22%2C%22amenityFilters%22%3Anull%2C%22bypassTargetings%22%3Afalse%2C%22categoryTag%22%3Anull%2C%22children%22%3Anull%2C%22disasterId%22%3Anull%2C%22discountedGuestFeeVersion%22%3Anull%2C%22displayExtensions%22%3Anull%2C%22federatedSearchId%22%3Anull%2C%22forceBoostPriorityMessageType%22%3Anull%2C%22hostPreview%22%3Afalse%2C%22infants%22%3Anull%2C%22interactionType%22%3Anull%2C%22layouts%22%3A%5B%22SIDEBAR%22%2C%22SINGLE_COLUMN%22%5D%2C%22pets%22%3A0%2C%22pdpTypeOverride%22%3Anull%2C%22photoId%22%3Anull%2C%22preview%22%3Afalse%2C%22previousStateCheckIn%22%3Anull%2C%22previousStateCheckOut%22%3Anull%2C%22priceDropSource%22%3Anull%2C%22privateBooking%22%3Afalse%2C%22promotionUuid%22%3Anull%2C%22relaxedAmenityIds%22%3Anull%2C%22searchId%22%3Anull%2C%22selectedCancellationPolicyId%22%3Anull%2C%22selectedRatePlanId%22%3Anull%2C%22splitStays%22%3Anull%2C%22staysBookingMigrationEnabled%22%3Afalse%2C%22translateUgc%22%3Anull%2C%22useNewSectionWrapperApi%22%3Afalse%2C%22sectionIds%22%3Anull%2C%22checkIn%22%3Anull%2C%22checkOut%22%3Anull%2C%22p3ImpressionId%22%3A%22p3_1729488926_P3eUXvpmVbFyX_Cq%22%7D%7D&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%223bcbe9ee20da9daa6722934f0b54ef68db41699d179e698c3aeab4ec30a964cb%22%7D%7D" headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36' # 可补充从浏览器复制的Cookie、X-Airbnb-API-Key等字段 } try: req = urllib.request.Request(api_url, headers=headers) jsonurl = urllib.request.urlopen(req) data = json.loads(jsonurl.read()) print(json.dumps(data, indent=4)) except Exception as e: print(f"Error fetching data: {e}") fetch_airbnb_data()
注意事项
- 私有接口随时可能变更,且未经授权使用可能违反Airbnb服务条款,商业场景务必使用官方API。
- 模拟浏览器请求时,Cookie等验证字段可能会过期,需要定期更新。
内容的提问来源于stack exchange,提问作者SailingHobo
相关产品推荐
相关产品推荐

