调用蒙特利尔STM的GTFS API遇400错误,求排查思路
蒙特利尔STM GTFS API请求400错误排查与解决
我在使用蒙特利尔地铁公司(STM)的GTFS交通API时,运行以下Python代码收到400错误(提示“请求过多”),希望有相关API使用经验的人帮忙排查问题。
import requests api_key = "API-KEY-FROM-DEVELOPER-ACCOUNT" api_url = f"https://api.stm.info/pub/od/gtfs-rt/ic/v2/vehiclePositions" # Prepare the request headers with the API key headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", } # Send the HTTP GET request to the API endpoint response = requests.get(api_url, headers=headers) # if you get 200 as response... everything ok print("response.status_code",response.status_code) # Check if the request was successful (status code 200) if response.status_code == 200: try: # Parse the JSON response data = response.json() # Process and use the retrieved data as needed print(data) except ValueError as e: print("Error parsing JSON:", e) print("Response content:", response.text) else: # Handle unsuccessful request print(f"Error: {response.status_code}")
我期望上述代码能返回status_code 200。
后续更新:实际问题出在认证方式,将请求头修改为以下内容后成功获取200响应,问题已解决。
headers = { "apiKey":api_key, "Content-Type": "application/json" }
感谢所有提供帮助的人,特别感谢ChatGPT!
内容的提问来源于stack exchange,提问作者oriolsierra
相关产品推荐
相关产品推荐

