【问题标题】:HMS Core: HTTP Request to Map kit via Python scriptHMS Core:通过 Python 脚本对地图套件的 HTTP 请求
【发布时间】:2020-12-26 00:55:42
【问题描述】:

我正在尝试通过 python 3 脚本向 Map kit API 发送 http/https 请求:

origin = {
    "lng": -4.66529,
    "lat": 54.216608
}
destination = {
    "lng": -4.66552,
    "lat": 54.2166
}
data_input = {
    "origin": origin,
    "destination": destination
}
json_input = json.dumps(data_input)

url = "https://mapapi.cloud.huawei.com/mapApi/v1/routeService/driving"   
headers = {"api-key": self.key,
           "Content-Type": "application/json"}
           
http_proxy = "http://proxy_ip:proxy_port"
https_proxy = "http://proxy_ip:proxy_port" 

proxyDict = {
    "http": http_proxy,
    "https": https_proxy
}

response = requests.get(url=url, data=json_input, headers=headers, proxies=proxyDict, verify=False)


print(response.status_code)
print(response.json()) 

但我有两个主要问题:

  • SSL 验证错误:
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)
  • 如果我关闭 SSL 验证,这就是我得到的响应打印结果:
404
{'returnCode': '5', 'returnDesc': 'NOT_FOUND'}

对这些问题有任何想法吗?尤其是第二个?

【问题讨论】:

  • 关于 SSL 认证检查错误,您是否尝试在代码中不使用代理配置?
  • 在第一种情况下,我尝试不使用代理配置,但遇到了同样的问题。此外,我在谷歌中找不到代码错误_ssl.c:1076
  • 类似的问题/答案对您有帮助吗? stackoverflow.com/questions/50236117/…
  • @AdiB:感谢您的建议,我已经遇到过这个问题/答案,但我在 Ubuntu 上,甚至安装了证书,但对我的问题没有帮助。

标签: python-requests huawei-mobile-services


【解决方案1】:

HUAWEI Map Kit Web API中的路线规划API是一套基于HTTPS的API,用于规划步行、骑行、驾车路线以及计算路线距离。 API 以 JSON 格式返回规划路线,并提供规划步行、骑自行车和驾车路线的功能。

API 参考:Walking Route Planning

解决方案:

您的代码中有两个问题:

  1. 使用 GET 请求而不是 POST 请求。 POST 请求必须用于路线规划。
  2. URL 中缺少必需的密钥。建议您将密钥合并到 URL 中。示例代码如下:
origin = {
"lng": -4.66529,
"lat": 54.216608
}
destination = {
"lng": -4.66552,
"lat": 54.2166
}
data_input = {
"origin": origin,
"destination": destination
}
json_input = json.dumps(data_input)

# replace with your own api_key, this api_key is not complete
url = "https://mapapi.cloud.huawei.com/mapApi/v1/routeService/driving?key=CV7bZ85W6Y4m%2f6fGZStNnquSLeYmJukcjeD9uJgKBRcZCg25dF%2f4cWeA5CQfWxQOKe2ByIaeEkwmMIPGBW5pPu0T%2"
headers = {"Content-Type": "application/json"}

# http_proxy = "http://proxy_ip:proxy_port"
# https_proxy = "http://proxy_ip:proxy_port"
#
# proxyDict = {
# "http": http_proxy,
# "https": https_proxy
# }

response = requests.post(url=url, data=json_input, headers=headers, verify=False)

print(response.status_code)
print(response.json())

那么,就可以成功获取请求结果了。

200
{'routes': [{'paths': [{'duration': 2.0, 'durationText': '1min', 'durationInTrafficText': '1min', 'durationInTraffic': 2.0, 'distance': 13.0, 'startLocation': {'lng': -4.6652902, 'lat': 54.21660782}, 'startAddress': 'German, Isle of Man, the United Kingdom', 'distanceText': '13m', 'steps': [{'duration': 1.0, 'orientation': 0, 'durationText': '1min', 'distance': 12.078, 'startLocation': {'lng': -4.6652902, 'lat': 54.21660782}, 'instruction': '', 'action': 'end', 'distanceText': '12m', 'endLocation': {'lng': -4.66544603, 'lat': 54.21666592}, 'polyline': [{'lng': -4.6652902, 'lat': 54.21660782}, {'lng': -4.66529083, 'lat': 54.21660806}, {'lng': -4.66529083, 'lat': 54.21660806}, {'lng': -4.66540472, 'lat': 54.21665}, {'lng': -4.66544603, 'lat': 54.21666592}], 'roadName': 'Poortown Road'}], 'endLocation': {'lng': -4.66544603, 'lat': 54.21666592}, 'endAddress': 'German, Isle of Man, the United Kingdom'}], 'bounds': {'southwest': {'lng': -4.66552194, 'lat': 54.21584278}, 'northeast': {'lng': -4.66216583, 'lat': 54.21669556}}}], 'returnCode': '0', 'returnDesc': 'OK'}

更新:

Here 是结果代码。

如果收到 401 响应,可能的原因如下:

【讨论】:

  • 感谢您的回答。我已经相应地更新了我的代码(使用 POST 并将密钥直接放在 URL 中),但我得到了这个响应:401 {'returnCode': '6', 'returnDesc': 'REQUEST_DENIED'}。正如 cmets 中提到的@Adi B,它应该引用 Unauthorized API call,但我怀疑是代理问题。
  • 我犯了一个愚蠢的错误:没有对密钥进行编码。现在我用urllib.parse.quote(key) 替换连接的键,我可以得到响应。
  • @Betty 太好了。我已更新我的答案以提醒更多开发人员。如果您有更多问题,请随时与我联系。
【解决方案2】:

对于第二个问题,您需要执行 POST 而不是 GET。

【讨论】:

  • 改为 POST 给出了这个答案,而不是 401 {'returnCode': '6', 'returnDesc': 'REQUEST_DENIED'}
  • 现在您正在使用预期的方法 (POST),您可以根据已知情况检查错误响应:developer.huawei.com/consumer/en/doc/development/… Unauthorized API call 表示请求中缺少 API 密钥。 developer.huawei.com/consumer/en/doc/development/… 向您展示如何将键添加为查询参数:https://mapapi.cloud.huawei.com/mapApi/v1/routeService/driving?key=API KEY
  • 我更改了将密钥传递给请求帖子的方式,但仍然遇到同样的问题,我怀疑是代理问题。
  • 使用urllib.parse.quote(key)对密钥进行编码现在提供预期的响应。
猜你喜欢
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-13
相关资源
最近更新 更多