Get credentials
HERE offers two different options for authentication when you sign up for a plan at the HERE website to use the global the version of HERE Geocoding and Search.
For the available authentication options, see the Identity & Access Management Developer Guide.
How to test your credentials
The python script hgs_access_test.py
below can be used to test the access to HERE Geocoding and Search (global version) using the Oauth tokens authentication method.
It requires both requests
and requests_oauthlib
packages.
Example of use
hgs_access_test.py
takes the access key id and access key secret of your credentials.properties
file to fire a query for "Berlin" against the /geocode
endpoint. A result similar to below is expected:
$ python hgs_access_test.py {YOUR_KEY_ID} {YOUR_KEY_SECRET}
(...)
send: b'POST /oauth2/token HTTP/1.1\r\nHost: account.api.here.com\r\nUser-Agent: python-requests/2.22.0\r\nAccept-Encoding: gzip, ...
send: b'{"grantType": "client_credentials", "clientId": ...
reply: 'HTTP/1.1 200 OK\r\n'
(...)
DEBUG:urllib3.connectionpool:https://account.api.here.com:443 "POST /oauth2/token HTTP/1.1" 200 879
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): discover.search.hereapi.com:443
send: b'GET /v1/discover?at=52.521,13.3807&q=berlin HTTP/1.1\r\nHost: discover.search.hereapi.com\r\nUser-Agent: ...
reply: 'HTTP/1.1 200 OK\r\n'
(...)
DEBUG:urllib3.connectionpool:https://discover.search.hereapi.com:443 "GET /v1/discover?at=52.521,13.3807&q=berlin HTTP/1.1" 200 265
results:
{
"items": [
{
"title": "Berlin, Deutschland",
"id": "here:cm:namedplace:20187403",
"resultType": "locality",
"localityType": "city",
"address": {
"label": "Berlin, Deutschland",
"countryCode": "DEU",
"countryName": "Deutschland",
"state": "Berlin",
"county": "Berlin",
"city": "Berlin",
"postalCode": "10117"
},
"position": {
"lat": 52.51604,
"lng": 13.37691
},
"distance": 608,
"mapView": {
"west": 13.08835,
"south": 52.33812,
"east": 13.761,
"north": 52.6755
}
}
]
}
hgs_access_test.py
source code
from sys import argv, stderr, exit
from json import dumps
import logging
import http.client
http.client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger('requests.packages.urllib3')
requests_log.propagate = True
requests_log.setLevel(logging.DEBUG)
try:
from requests_oauthlib import OAuth1
except ImportError as e:
raise ImportError(f'{e.msg}\nPackage available at https://pypi.org/project/requests-oauthlib')
try:
from requests import get, post, HTTPError
except ImportError as e:
raise ImportError(f'{e.msg}\nPackage available at https://pypi.org/project/requests')
usage = """Usage:
hgs_access_test.py <key-id> <key_secret>
"""
search_query = 'https://discover.search.hereapi.com/v1/discover?at=52.521,13.3807&q=berlin'
try:
data = {
'grantType': 'client_credentials',
'clientId': argv[1],
'clientSecret': argv[2]
}
except IndexError:
stderr.write(usage)
exit(1)
response = post(
url='https://account.api.here.com/oauth2/token',
auth=OAuth1(argv[1], client_secret=argv[2]) ,
headers= {'Content-type': 'application/json'},
data=dumps(data)).json()
try:
token = response['accessToken']
token_type = response['tokenType']
expire_in = response['expiresIn']
except KeyError as e:
print(dumps(response, indent=2))
exit(1)
headers = {'Authorization': f'{token_type} {token}'}
search_results = dumps(get(search_query, headers=headers).json(), indent=2)
print(f'results:\n{search_results}')