我正在尝试开始使用从 Python 调用的 Bing 数据流 API 来批量地理编码地址。为简单起见,我使用 csv 输入格式。我经常遇到 400 状态代码“一个或多个参数无效”。 为了尝试获得一个有效的示例,我使用直接从文档复制的 Microsoft 示例 csv 输入数据(网页:地理编码数据流示例输入和输出数据版本 2.0)。这是我的测试代码:
def _testApiCall():
payload = "Bing Spatial Data Services, 2.0\n"
payload += "Id, GeocodeRequest/Culture, *plus the rest of the long line of field names*
payload += "1,en-US,,One Microsoft Way,WA,,,,Redmond,98052\n"
payload += "3,en-US,One Microsoft Way, Redmond, Wa\n"
payload += "4,en-gb"
url = f"https://spatial.virtualearth.net/REST/v1/dataflows/geocode?input=csv&output=json&key={bingMapsKey}"
response = requests.post(url, data=payload.encode('utf-8'))
with open("response.json", "w") as response_file:
json.dump(response.json(), response_file, indent=4)
响应文件始终包含:
{
"authenticationResultCode": "ValidCredentials",
"brandLogoUri": ...,
"copyright": ...,
"errorDetails": [
"One or more parameters are not valid.",
": The data uploaded in this request was missing or invalid."
],
"resourceSets": [],
"statusCode": 400,
"statusDescription": "Bad Request",
"traceId": "a45bd0371d8e4a55b5e415305710ef80|BN00006676|0.0.0.0"
}
无法弄清楚我做错了什么,或者它引用了什么参数。
我已将调用的代码减少到最低限度,并复制了 MS 的示例数据。我的 API 密钥已经过验证,因此显然 URL 基本上没问题。期望得到包含作业 ID 和下载数据链接的正常结果。似乎无法超越状态 400 失败。我缺少什么?谢谢。
问题是 requests.post() 中缺少 Content-Type 标头规范