python-requests - HMS Core:通过 Python 脚本对地图套件的 HTTP 请求
问题描述
我正在尝试通过 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'}
对这些问题有任何想法吗?尤其是第二个?
解决方案
HUAWEI Map Kit Web API中的路线规划API是一套基于HTTPS的API,用于规划步行、骑行、驾车路线以及计算路线距离。API 以 JSON 格式返回规划路线,并提供规划步行、骑自行车和驾车路线的功能。
API参考:步行路线规划
解决方案:
您的代码中有两个问题:
- 使用 GET 请求而不是 POST 请求。POST 请求必须用于路线规划。
- 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'}
更新:
这是结果代码。
如果收到 401 响应,可能的原因如下:
- 您的项目中的应用 ID 与AppGallery Connect中的不同。
- 未配置签名证书指纹。您需要生成签名证书指纹并在 AppGallery Connect 中进行配置。
- 您的应用的AppGallery Connect配置文件未配置。您需要按照添加您的应用的AppGallery Connect配置文件中的说明进行配置。
- API 密钥未使用 encode 进行转码。
推荐阅读
- javascript - Python Selenium - how to click full screen button of a video without a frame
- scala - 基于键合并两列结构数组
- python - 如何获取 scipy.optimize.linprog 的结果数组?
- r - 填充值未映射到正确的因子 ggplot
- excel - COUNTIFS formula - unique values
- regex - 正则表达式 - 从起点到终点抓取单词
- android - OSM 奖励包 - OSRMRoadManager::getRoad: 请求失败
- javascript - Web 应用程序如何将浏览器带到前台?
- javascript - 防止长按选择附近的文本
- markdown - Pandoc:如何在将 md 渲染为 pdf 时保持图像和字幕的顺序