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

UberX精确报价获取疑问:APP可查但Python API仅见价格范围

获取UberX精确报价的Python API实现步骤

我来帮你搞定这个问题——其实你之前用的应该是Uber API里的普通价格范围查询接口,而要拿到和APP内一样的精确实时报价,得用**请求预估(Request Estimate)**接口,这才是对应APP内即时请求的API能力。下面是具体的实现步骤:

1. 确认开发者账号权限

首先得确保你的Uber开发者账号已经启用了request_estimate相关的权限(在Uber开发者控制台的应用设置里可以配置),因为这个接口需要获取实时路线、供需数据,权限要求比普通价格范围查询更高。

2. 获取UberX的产品ID

在调用精确报价接口之前,你需要先拿到目标区域内UberX对应的product_id,可以通过产品列表接口获取:

import requests

# 替换成你的Uber API访问令牌
ACCESS_TOKEN = "your_access_token_here"
headers = {"Authorization": f"Bearer {ACCESS_TOKEN}"}

# 设置起点坐标(示例为旧金山市区)
location_params = {
    "latitude": 37.7749,
    "longitude": -122.4194
}

# 请求该区域的可用Uber产品
products_response = requests.get(
    "https://api.uber.com/v1.2/products",
    headers=headers,
    params=location_params
)
products_data = products_response.json()

# 遍历找到UberX的product_id
uberx_product_id = None
for product in products_data.get("products", []):
    if product["display_name"] == "UberX":
        uberx_product_id = product["product_id"]
        break

3. 调用请求预估接口获取精确报价

拿到product_id后,就可以调用POST /v1.2/requests/estimate接口,传入起点、终点和产品ID,就能获取和APP一致的精确报价了:

if uberx_product_id:
    # 设置起点和终点坐标
    estimate_params = {
        "start_latitude": 37.7749,
        "start_longitude": -122.4194,
        "end_latitude": 37.7898,
        "end_longitude": -122.401,
        "product_id": uberx_product_id
    }

    # 请求精确报价
    estimate_response = requests.post(
        "https://api.uber.com/v1.2/requests/estimate",
        headers=headers,
        json=estimate_params
    )
    estimate_data = estimate_response.json()

    # 提取并打印精确报价
    if "fare" in estimate_data:
        exact_fare = estimate_data["fare"]["display"]
        print(f"当前UberX的精确报价:{exact_fare}")
    else:
        print("无法获取精确报价,请检查参数或权限是否正确")
else:
    print("当前区域暂不提供UberX服务")

关键注意事项

  • 这个接口返回的fare字段包含实时计算的精确价格(和APP内显示一致),而不是范围值,因为它会结合实时路况、供需情况和具体路线计算。
  • 确保你的访问令牌(ACCESS_TOKEN)有足够的权限,如果遇到权限错误,回到开发者控制台检查scope配置。
  • 部分地区可能因当地法规限制,即使调用该接口也只能返回价格范围,但大部分支持即时报价的地区都能拿到精确值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:09:06