HERE 矩阵路由 API - "Method not allowed for this action"

HERE Matrix Routing API - "Method not allowed for this action"

我正在尝试让 reference example 工作,但是 API 抛出 405 错误:

import requests
import json

url = 'https://matrix.router.hereapi.com/v8/matrix'
headers = {'Content-Type': 'application/json'}
payload = {
    "apiKey": <API_KEY>,
    "origins": [
        {"lat": 52.52103, "lng": 13.41268},
        {"lat": 52.51628, "lng": 13.37771},
        {"lat": 52.47342, "lng": 13.40357}
    ],
    "regionDefinition": {
        "type": "boundingBox",
        "north": 52.53,
        "south": 52.46,
        "west": 13.35,
        "east": 13.42
    },
    "matrixAttributes": ["distances"]
}

r = requests.get(url, headers=headers, params=payload)

print(json.dumps(r.json(), indent=2))

然而,这给出了

{"error":"Method not allowed for this action","error_description":"Method not allowed for this action"}

我的 r.urlhttps://matrix.router.hereapi.com/v8/matrix?apiKey=API_KEY&origins=lat&origins=lng&origins=lat&origins=lng&origins=lat&origins=lng&regionDefinition=type&regionDefinition=north&regionDefinition=south&regionDefinition=west&regionDefinition=east&matrixAttributes=distances

这显然看起来不对,所以我尝试将我的代码修改为

r = requests.get(url, headers=headers, params=json.dumps(payload))

这会产生 https://matrix.router.hereapi.com/v8/matrix?%7B%22apiKey%22:%20%22API_KEY%22,%20%22origins%22:%20[%7B%22lat%22:%2052.52103,%20%22lng%22:%2013.41268%7D,%20%7B%22lat%22:%2052.51628,%20%22lng%22:%2013.37771%7D,%20%7B%22lat%22:%2052.47342,%20%22lng%22:%2013.40357%7D],%20%22regionDefinition%22:%20%7B%22type%22:%20%22boundingBox%22,%20%22north%22:%2052.53,%20%22south%22:%2052.46,%20%22west%22:%2013.35,%20%22east%22:%2013.42%7D,%20%22matrixAttributes%22:%20[%22distances%22]%7D

r.url

这个也不对,我试了最简单的例子

import requests
import json

url = 'https://matrix.router.hereapi.com/v8/matrix?apiKey=API_KEY&async=false&regionDefinition=world&profile=truckFast'
headers = {'Content-Type': 'application/json'}

r = requests.get(url, headers=headers)

print(json.dumps(r.json(), indent=2))

这会产生 https://matrix.router.hereapi.com/v8/matrix?apiKey=API_KEY&async=false&regionDefinition=world&profile=truckFast

r.url

在所有这些示例中,API 仍然是 returns

{"error":"Method not allowed for this action","error_description":"Method not allowed for this action"}

为什么会这样?

以防万一,我的 requests.__version__ 是 2.27.1.

使用POST方法提交路由计算矩阵:

https://matrix.router.hereapi.com/v8/matrix?regionDefinition=world&profile=truckFast&apiKey=

另请参考矩阵路由API示例

看起来问题确实是使用 GET 而不是 POST。但是,通过代码示例发布更完整的答案,以解决有关如何设置有效负载等问题。

import requests
import json

url = 'https://matrix.router.hereapi.com/v8/matrix?apiKey=<API_KEY>&async=false'
headers = {'Content-Type': 'application/json'}

payload = {
    "origins": [
        {"lat": 52.52103, "lng": 13.41268},
        {"lat": 52.51628, "lng": 13.37771},
        {"lat": 52.47342, "lng": 13.40357}
    ],
    "regionDefinition": {
        "type": "boundingBox",
        "north": 52.53,
        "south": 52.46,
        "west": 13.35,
        "east": 13.42
    },
    "matrixAttributes": ["distances"]
}

r = requests.post(url, headers=headers, data=json.dumps(payload))

print(json.dumps(r.json(), indent=2))

这导致

{
  "matrixId": "fdaa07ea-f8d9-4b3f-a410-9c3b2d54e464",
  "matrix": {
    "numOrigins": 3,
    "numDestinations": 3,
    "distances": [
      0,
      2924,
      7258,
      3221,
      0,
      7710,
      7333,
      7721,
      0
    ]
  },
  "regionDefinition": {
    "type": "boundingBox",
    "west": 13.35,
    "south": 52.46,
    "east": 13.42,
    "north": 52.53
  }
}