如何使用此处的异步 Matrix Routing API v8 来获取包括交通在内的行程时间响应

问题描述 投票:0回答:2

我关注一组节点之间(双向)的旅行时间,包括交通。在旧版本的 API (7.2) 中,我将在 Python 中使用以下代码来请求:

    payload = {
        "apiKey": "API_KEY_HERE",
        "start0": "-33.795602,151.185366",
        "start1": "-33.865103,151.205627",
        "destination0": "-33.795602,151.185366",
        "destination1": "-33.865103,151.205627",
        "mode": "fastest;car;traffic:enabled",
        "departure": "2020-10-27T08:00:00+11",
        "summaryattributes": "all"
    }
    base_url = "https://matrix.route.ls.hereapi.com/routing/7.2/calculatematrix.json?"
    r = requests.get(base_url, params=payload)
    print(r.json())

新版本的例子较少,说实话我对POST和异步响应不太熟悉。

为什么要换新版本?好吧,看来您只能提供一组原始节点/位置,然后将计算一个矩阵(在后台),一旦准备好,就可以通过 GET 请求拉取它。没有指定start0、start1、..等

版本 8 的新尝试:

步骤:

  1. 请求矩阵(POST)
  2. 轮询状态 (GET)
  3. 准备好后下载结果(GET)
# 1. Request matrix (POST)
    base_url = "https://matrix.router.hereapi.com/v8/matrix?"
    params = {"apiKey": "MY_API_KEY_HERE",
              "async": "true",
              "departureTime": "2020-10-27T08:00:00+11"}
    payload = {"origins": [{"lat": -33.759688, "lng": 151.156369}, {"lat": -33.865189, "lng": 151.208162},
                           {"lat": -33.677066, "lng": 151.302117}],
               "regionDefinition": {"type": "autoCircle", "margin": 10000},
               "matrixAttributes": ["travelTimes", "distances"]}
    headers = {'Content-Type': 'application/json'}
    r = requests.post(base_url, params=params, json=payload, headers=headers)
    response = r.json()
    
    # pretty print
    print(json.dumps(response, indent=4))

这给出了“已接受”状态:

    // Example response
    {
        "matrixId": "eba6780c-0379-40f1-abaf-5c62d07dabb4",
        "status": "accepted",
        "statusUrl": "https://aws-eu-west-1.matrix.router.hereapi.com/v8/matrix/eba6780c-0379-40f1-abaf-5c62d07dabb4/status"
    }

然后我使用 statusUrl 和 apiKey 来轮询状态。这就是我被困住的地方。我应该如何验证?我不确定身份验证应该如何工作。第 1 步中的身份验证成功了。

# 2. Poll for status (GET)
    time.sleep(3)
    statusUrl = response['statusUrl']
    params = {'apiKey': 'MY_API_KEY_HERE'}
    headers = {'Content-Type': 'application/json'}
    r = requests.get(statusUrl, params=params, headers=headers)
    response = r.json()
    
    # pretty print
    print(json.dumps(response, indent=4))

在写入“MY_API_KEY_HERE”的地方,我输入我的 apiKey。回复:

{
    "error": "Unauthorized",
    "error_description": "No credentials found"
}

显然,使用身份验证时出现错误。认证应该如何使用?是否可以展示检查已提交矩阵计算状态的成功请求如何以及如何在 Python 中下载此类矩阵的请求(使用 gzip 标头轮询状态后的下一步)? 欢迎任何指点。

python post matrix here-api travel-time
2个回答
1
投票

这将计算行程时间,包括交通时间: 方法网址:

https://matrix.router.hereapi.com/v8/matrix?apiKey={{API_KEY}}&async=false

方法:

POST

身体:

{ "origins": [ { "lat": 47.673993, "lng": -122.356108 }, { "lat": 47.656910, "lng": -122.362823 }, { "lat": 47.648015, "lng": -122.335674 }, { "lat": 47.653022, "lng": -122.312461 }, { "lat":47.675796, "lng": -122.311520 } ], "destinations": [ { "lat": 47.661438, "lng": -122.336427 } ], "regionDefinition": { "type": "world" } }



0
投票

解决方案是在您的请求中添加

allow_redirects=False

,按照

here
(由在here.com工作的人)给出的答案,解决与此处发布的问题几乎相同的问题。 这已经为我解决了。

© www.soinside.com 2019 - 2024. All rights reserved.