Google maps bad request
-
Hi All, I'm trying to get some details back from the Google maps API but it is coming back with a 400 bad request error when run on my M5 Paper. The URL works anywhere else I try it. Other URLs for other services work perfectly. Can anyone help with this code please.
import os, sys, io
import M5
from M5 import *
import requests2
import networklabel0 = None
http_req = None
wlan = Nonemapsurl = None
headers = None
payload = Nonedef getETA():
global payload, headers, mapsurl, label0, wlan, http_req
label0.setText(str('Label'))
payload = {}
headers = {}
http_req = requests2.get(mapsurl, headers=headers, data=payload)
label0.setText(str(http_req.text))def setup():
global label0, http_req, wlan, mapsurl, headers, payloadM5.begin()
Widgets.fillScreen(0xeeeeee)
label0 = Widgets.Label("label0", 20, 93, 1.0, 0x000000, 0xeeeeee, Widgets.FONTS.DejaVu24)wlan = network.WLAN(network.STA_IF)
mapsurl = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=32 London Bridge St, London SE1 9SG&destinations=50 Lower Thames Street, London, EC3R 6DT&departure_time=now&key=<google maps api key>'
wlan.connect('<Network SSID>', '<Network Password>')def loop():
global label0, wlan, http_req, payload, headers, mapsurl
M5.update()
if BtnA.isPressed():
getETA()if name == 'main':
try:
setup()
while True:
loop()
except (Exception, KeyboardInterrupt) as e:
try:
from utility import print_error_msg
print_error_msg(e)
except ImportError:
print("please update to latest firmware") -
Hi All, I was actually using a method that is a little old. This has now been replaced. I've fleshed this out a little more to show only the duration but the working code is below. Replacing 'routes.duration' with '*' will return the full trip including turn by turn.
import os, sys, io
import M5
from M5 import *
import requests2
import network
import json
import requests
import mathrect0 = None
label1 = None
label0 = None
http_req = None
wlan = Noneurl = None
responsejson = None
timeseconds = None
sliced = Nonedef getETA():
global url, responsejson, timeseconds, label0, rect0, label1, http_req, wlan, sliced
payload = json.dumps({
"origin": {
"address": "32 London Bridge St, London SE1 9SG"
},
"destination": {
"address": "50 Lower Thames Street, London, EC3R 6DT"
},"routingPreference": "TRAFFIC_AWARE"
})
headers = {
'X-Goog-FieldMask': 'routes.duration',
'Content-Type': 'application/json',
'X-Goog-Api-Key': '{{GMP API Key}}'
}response = requests.request("POST", url, headers=headers, data=payload)
responsejson = json.loads((response.text))
timeseconds = responsejson["routes"][0]["duration"]
#sliced = slice(0,4)
#print(timeseconds[slice(4)])
print(str(math.trunc(int(timeseconds.strip("s"))/60)) + "m" )
label0.setText(str(math.trunc(int(timeseconds.strip("s"))/60)) + "m" )def setup():
global label0, rect0, label1, http_req, wlan, url, timeseconds, responsejson, slicedM5.begin()
Widgets.fillScreen(0xeeeeee)
label0 = Widgets.Label("Start", 317, 99, 1.0, 0x000000, 0xeeeeee, Widgets.FONTS.DejaVu72)
rect0 = Widgets.Rectangle(301, 52, 199, 199, 0x000000, 0xeeeeee)
label1 = Widgets.Label("Pitacs ETA", 336, 207, 1.0, 0x000000, 0xeeeeee, Widgets.FONTS.DejaVu24)
wlan = network.WLAN(network.STA_IF)
wlan.connect('<Network SSID>', '<Network Password>')
url = 'https://routes.googleapis.com/directions/v2:computeRoutes?key=<google maps api key>'def loop():
global label0, rect0, label1, http_req, wlan, url, timeseconds, responsejson, sliced
M5.update()
if BtnA.isPressed():
getETA()if name == 'main':
try:
setup()
while True:
loop()
except (Exception, KeyboardInterrupt) as e:
try:
from utility import print_error_msg
print_error_msg(e)
except ImportError:
print("please update to latest firmware")