How to insert api_key into Python script?


#1

Hi, support!

How to insert api_key into Python script?
Here is code:

from urllib2 import Request, urlopen

values = “”"
{“coordinates”:[[8.681495,49.41461],[8.686507,49.41943],[8.687872,49.420318]]}"""

#headers = {
# ‘Accept’: 'application/json; charset=utf-8’
#}

headers = {‘Authorization’: ‘your-key’}

request = Request('https://api.openrouteservice.org/v2/directions/driving-car/gpx?’, data=values, headers=headers)

response_body = urlopen(request).read()
print response_body

I get error: urllib2.HTTPError: HTTP Error 500: Internal Server Error

What do I wrong?


#2

That you’re not using openrouteservice-py;)

pip install openrouteservice


#3

@Janex72 please never post your key, always substitute it.

I will update the autogenerated example in the playground with something working like:

import requests

values = {"coordinates": [[8.681495, 49.41461], [8.686507, 49.41943], [8.687872, 49.420318]]}

headers = {
    'Accept': 'application/gpx+xml, application/geo+json, application/json; charset=utf-8',
    'Authorization': 'your-key'
}
call = requests.post('https://api.openrouteservice.org/v2/directions/driving-car/json', json=values, headers=headers)

print(call.text)