2

I have a valid HereMaps API Key and I am using python requests lib to execute a GET request to a link that is returned after an async call is made. I am trying to execute it like this:

https://aws-ap-southeast-1.matrix.router.hereapi.com/v8/matrix/01187ff8-7888-4ed6-af88-c8f9be9242d7/status?apiKey={my-api-key}

Even though I have provided the proper key before to generate this link above, the result of this request above returns:

{
  "error": "Unauthorized",
  "error_description": "No credentials found"
}

How should we authenticate to check the status result for an async request on the Matrix Routing v8 API?

1
  • 1
    Might be that your problem comes from the automatic redirect and mixed up authentification methods: Search for "follow redirects automatically" in the spec matrix.router.hereapi.com/v8/openapi. Commented Jan 9, 2021 at 13:46

2 Answers 2

6

There are two authentication methods available to you for using HERE APIs: API key and OAuth token.

Because of the way credentials are being handled, when you make an asynchronous request, you'll need to disable automatic client redirects when using API key, as the client will not add the apiKey parameter again to the URL it has been redirected to.

There are many solutions to this problem when using Python and requests, and here's a complete example that might help:

import requests, time

api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# "Profile mode (car fastest)" example request
request = {
    "origins": [{"lat": 52.5309, "lng": 13.3849}, {"lat": 54.0924, "lng": 12.0991}],
    "destinations": [
        {"lat": 51.3397, "lng": 12.3731},
        {"lat": 51.0504, "lng": 13.7373},
    ],
    "regionDefinition": {"type": "world"},
    "profile": "carFast",
}

# Using a Session object allows you to persist certain parameters accross requests
# see: https://requests.readthedocs.io/en/master/user/advanced/
session = requests.Session()
# Add `?apiKey=xxxxxx` to all requests
session.params = {"apiKey": api_key}
# Raise an exception for any non 2xx or 3xx HTTP return codes
session.hooks = {"response": lambda r, *args, **kwargs: r.raise_for_status()}

# Send an asynchronous request, see: https://developer.here.com/documentation/matrix-routing-api/8.3.0/dev_guide/topics/get-started/asynchronous-request.html
status_response = session.post(
    "https://matrix.router.hereapi.com/v8/matrix", json=request
).json()

# Polling for the job status for 100 times
# You might want to use exponential back-off instead of time.sleep
for _ in range(0, 100):
    # do not follow the redirect here
    status_response = session.get(
        status_response["statusUrl"], allow_redirects=False
    ).json()

    if status_response["status"] == "completed":
        # download the result
        matrix_response = session.get(status_response["resultUrl"]).json()
        print(matrix_response)
        break
    elif status_response["accepted"] or status_response["inProgress"]:
        continue
    else:
        print(f"An error occured: {status_response}")
        break
    time.sleep(0.5)  # sleep for 500 ms

Disclaimer: I work at HERE Technologies on Matrix Routing.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for this really helpful, I am facing a issue, above example only returns the travel times, how do i get the travel distances as well?
Also, when i am entering 59 origins and 59 destinations, the same code is giving the following error: HTTPError: 403 Client Error: Forbidden for url: matrix.router.hereapi.com/v8/matrix?apiKey='My_API_Key' how do I resolve this? I tried using circle as region definition but still same error
To request distances as part of the calculation, you need to specify: json "matrixAttributes": ["distances"] in the request JSON, as specific in the API specification. As for the HTTP 403, you need to check in the body of the response, which will tell you the exact reason. In general, if you have another question, please make a new post (to be in accordance with the rules of StackOverflowe)
how do you disable automatic client redirect when using the rest api?
@GabrielFéron how to test this postman, I am getting same error for my GET request. I am working in oracle pl/sql and calling it as a REST web-service, so first exploring the API in postman.
|
-1

Please check the documentation for the domain name to use for HERE services. For Matrix Routing you should be using matrix.route.ls.hereapi.com.

Disclosure: I'm a product manager at HERE Technologies

1 Comment

I think you mixed up the products: the OP asks about matrix.router.hereapi.com/v8/openapi and not v7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.