Bing Maps Distance Matrix API错误:'内容:该参数丢失或无效。'

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

我正在尝试使用Bing的Maps Distance Matrix API的示例。该示例是从其网站(here)上“按原样”获取的。

获取和发布示例均失败,并出现以下错误:

{'authenticationResultCode': 'ValidCredentials',
 'brandLogoUri': 'http://dev.virtualearth.net/Branding/logo_powered_by.png',
 'copyright': 'Copyright © 2020 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.',
 'errorDetails': ['One or more parameters are not valid.',
  'content: This parameter is missing or invalid.'],
 'resourceSets': [],
 'statusCode': 400,
 'statusDescription': 'Bad Request',
 'traceId': '338104f79bff4dfe9b18d0333e80abe7|DU00000D6A|0.0.0.0'}

{'authenticationResultCode': 'ValidCredentials',
 'brandLogoUri': 'http://dev.virtualearth.net/Branding/logo_powered_by.png',
 'copyright': 'Copyright © 2020 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.',
 'errorDetails': ['JSON input is incorrect'],
 'resourceSets': [],
 'statusCode': 400,
 'statusDescription': 'Bad Request',
 'traceId': 'cbef56289d164cc1a691ad8f328e4cf3|DU00000B71|0.0.0.0'}   

这些请求是在Python中通过'requests'库发出的。这是调用get请求的示例:

url = "https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix?origins=47.6044,-122.3345;47.6731,-122.1185;47.6149,-122.1936&destinations=45.5347,-122.6231;47.4747,-122.2057&travelMode=driving&key={myKeyHere}"
r = req.post(url)
j = r.json()

这是邮寄要求:

url = "https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix?key={myKeyHereWithNoBrackets}"
headers = {
    "Content-Type":"application/json",
}
payload ={
    "origins": [{
        "latitude": 47.6044,
        "longitude": -122.3345
    },
    {
        "latitude": 47.6731,
        "longitude": -122.1185
    },
    {
        "latitude": 47.6149,
        "longitude": -122.1936
    }],
    "destinations": [{
        "latitude": 45.5347,
        "longitude": -122.6231
    }, 
    {
        "latitude": 47.4747,
        "longitude": -122.2057
    }],
    "travelMode": "driving"
}
response = req.post(url,headers=headers, data=payload)
json_obj = response.json()
json_obj

提前感谢!

python python-requests bing-maps
1个回答
0
投票

好,我已经解决了。用content-type:application/json发出POST请求时,必须将有效负载转换为json。为此,我修改了以下行:

response = req.post(url, headers=headers, data=payload)

至以下:

response = req.post(url, headers=headers, data=json.dumps(payload))

事实上,我还必须导入json库。

对于GET请求,我仍然不确定该怎么做。如果有人知道,请发表评论以更新回复或发布您自己的回复。

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