2

I am trying to send http/https requests to the Map kit API throughout a python 3 script:

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()) 

but I have two main issues:

  • An SSL cerification Error:
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)
  • If I turn off the SSL verification, this is what I get as a print of the response:
404
{'returnCode': '5', 'returnDesc': 'NOT_FOUND'}

Any idea about these issues? especially the second one?

Betty
  • 237
  • 6
  • 16
  • Regarding the SSL certification check error, did you try without the proxy config in your code? – Adi B Sep 08 '20 at 07:35
  • In first instances, I was trying without proxy configuration but was having the same issue. Moreover, I can't find the code error `_ssl.c:1076` in google. – Betty Sep 08 '20 at 08:26
  • Does a similar issue/answer help you? https://stackoverflow.com/questions/50236117/scraping-ssl-certificate-verify-failed-error-for-http-en-wikipedia-org – Adi B Sep 08 '20 at 09:22
  • @AdiB: thanks for the suggestion, I have already came accross that question/answer, but I am on Ubuntu, and even installed certifi but no help to my issue. – Betty Sep 08 '20 at 10:07

2 Answers2

2

Route planning APIs in Web APIs of HUAWEI Map Kit are a set of HTTPS-based APIs for planning walking, cycling, and driving routes and calculating route distances. The APIs return planned routes in JSON format, and provide the capabilities to plan walking, cycling, and driving routes.

API Reference: Walking Route Planning

Solution:

There are two issues in your code:

  1. The GET request instead of the POST request is used. The POST request must be used for route planning.
  2. The required key is missing in the URL. You are advised to incorporate the key into the URL. The sample code is as follows:
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())

Then, the request result can be obtained successfully.

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'}

Update:

Here is the Result Codes.

If you receive a 401 response, the possible causes are as follows:

  • The app ID in your project is different from that in AppGallery Connect.
  • No signing certificate fingerprint is configured. You need to generate a signing certificate fingerprint and configure it in AppGallery Connect.
  • The AppGallery Connect configuration file of your app is not configured. You need to follow instructions in Adding the AppGallery Connect Configuration File of Your App to configure it.
  • The API key is not transcoded using encode.
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • Thank you for your answer. I have updated my code accordingly (use POST and put the key direcly in the URL), but I'm having this response: `401 {'returnCode': '6', 'returnDesc': 'REQUEST_DENIED'}`. As @Adi B mentioned in the comments, it shlould refer to `Unauthorized API call`, but I am suspecting a proxy issue. – Betty Sep 08 '20 at 09:59
  • I was doing a silly mistake: not encoding the key. Now I am replacing the concatanated key by `urllib.parse.quote(key)` and I can get the response. – Betty Sep 08 '20 at 10:56
  • @Betty Great. I have updated my answer to remind more developers. If you have more questions, feel free to contact me. – zhangxaochen Sep 09 '20 at 00:31
  • Another possible cause for this error is having upgraded the wrong one billing plan, instead of the "Petal Maps Platform - Pay-as-you-go plan". – Martin Zeitler Mar 03 '22 at 02:26
1

For the second question, you need to do a POST instead of a GET.

Adi B
  • 819
  • 1
  • 8
  • 11
  • Changing to POST gives this answer instead `401 {'returnCode': '6', 'returnDesc': 'REQUEST_DENIED'}` – Betty Sep 08 '20 at 08:23
  • Now that you're using the expected method (POST), you can check the error response against the know situations: https://developer.huawei.com/consumer/en/doc/development/HMSCore-References-V5/error-code-0000001050161430-V5 `Unauthorized API call` means that the API key is missing from the request. https://developer.huawei.com/consumer/en/doc/development/HMSCore-References-V5/directions-driving-0000001050161496-V5 shows you how to add the key as a query parameter: `https://mapapi.cloud.huawei.com/mapApi/v1/routeService/driving?key=API KEY` – Adi B Sep 08 '20 at 09:10
  • I have changed the way I am passing the key to the request post, but still getting the same issue, I am suspecting a proxy problem. – Betty Sep 08 '20 at 09:56
  • 1
    Encording the key provides now with the expected response using `urllib.parse.quote(key)`. – Betty Sep 08 '20 at 11:13