<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Google maps bad request]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">import os, sys, io<br />
import M5<br />
from M5 import *<br />
import requests2<br />
import network</p>
<p dir="auto">label0 = None<br />
http_req = None<br />
wlan = None</p>
<p dir="auto">mapsurl = None<br />
headers = None<br />
payload = None</p>
<p dir="auto">def getETA():<br />
global payload, headers, mapsurl, label0, wlan, http_req<br />
label0.setText(str('Label'))<br />
payload = {}<br />
headers = {}<br />
http_req = requests2.get(mapsurl, headers=headers, data=payload)<br />
label0.setText(str(http_req.text))</p>
<p dir="auto">def setup():<br />
global label0, http_req, wlan, mapsurl, headers, payload</p>
<p dir="auto">M5.begin()<br />
Widgets.fillScreen(0xeeeeee)<br />
label0 = Widgets.Label("label0", 20, 93, 1.0, 0x000000, 0xeeeeee, Widgets.FONTS.DejaVu24)</p>
<p dir="auto">wlan = network.WLAN(network.STA_IF)<br />
mapsurl = '<a href="https://maps.googleapis.com/maps/api/distancematrix/json?origins=32" target="_blank" rel="noopener noreferrer nofollow ugc">https://maps.googleapis.com/maps/api/distancematrix/json?origins=32</a> London Bridge St, London SE1 9SG&amp;destinations=50 Lower Thames Street, London, EC3R 6DT&amp;departure_time=now&amp;key=&lt;google maps api key&gt;'<br />
wlan.connect('&lt;Network SSID&gt;', '&lt;Network Password&gt;')</p>
<p dir="auto">def loop():<br />
global label0, wlan, http_req, payload, headers, mapsurl<br />
M5.update()<br />
if BtnA.isPressed():<br />
getETA()</p>
<p dir="auto">if <strong>name</strong> == '<strong>main</strong>':<br />
try:<br />
setup()<br />
while True:<br />
loop()<br />
except (Exception, KeyboardInterrupt) as e:<br />
try:<br />
from utility import print_error_msg<br />
print_error_msg(e)<br />
except ImportError:<br />
print("please update to latest firmware")</p>
]]></description><link>https://community.m5stack.com/topic/7420/google-maps-bad-request</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 16:18:24 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/7420.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 21 Mar 2025 14:58:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Google maps bad request on Sat, 22 Mar 2025 13:49:53 GMT]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">import os, sys, io<br />
import M5<br />
from M5 import *<br />
import requests2<br />
import network<br />
import json<br />
import requests<br />
import math</p>
<p dir="auto">rect0 = None<br />
label1 = None<br />
label0 = None<br />
http_req = None<br />
wlan = None</p>
<p dir="auto">url = None<br />
responsejson = None<br />
timeseconds = None<br />
sliced = None</p>
<p dir="auto">def getETA():<br />
global url, responsejson, timeseconds, label0, rect0, label1, http_req, wlan, sliced<br />
payload = json.dumps({<br />
"origin": {<br />
"address": "32 London Bridge St, London SE1 9SG"<br />
},<br />
"destination": {<br />
"address": "50 Lower Thames Street, London, EC3R 6DT"<br />
},</p>
<p dir="auto">"routingPreference": "TRAFFIC_AWARE"<br />
})<br />
headers = {<br />
'X-Goog-FieldMask': 'routes.duration',<br />
'Content-Type': 'application/json',<br />
'X-Goog-Api-Key': '{{GMP API Key}}'<br />
}</p>
<p dir="auto">response = requests.request("POST", url, headers=headers, data=payload)<br />
responsejson = json.loads((response.text))<br />
timeseconds = responsejson["routes"][0]["duration"]<br />
#sliced = slice(0,4)<br />
#print(timeseconds[slice(4)])<br />
print(str(math.trunc(int(timeseconds.strip("s"))/60)) + "m" )<br />
label0.setText(str(math.trunc(int(timeseconds.strip("s"))/60)) + "m" )</p>
<p dir="auto">def setup():<br />
global label0, rect0, label1, http_req, wlan, url, timeseconds, responsejson, sliced</p>
<p dir="auto">M5.begin()<br />
Widgets.fillScreen(0xeeeeee)<br />
label0 = Widgets.Label("Start", 317, 99, 1.0, 0x000000, 0xeeeeee, Widgets.FONTS.DejaVu72)<br />
rect0 = Widgets.Rectangle(301, 52, 199, 199, 0x000000, 0xeeeeee)<br />
label1 = Widgets.Label("Pitacs ETA", 336, 207, 1.0, 0x000000, 0xeeeeee, Widgets.FONTS.DejaVu24)<br />
wlan = network.WLAN(network.STA_IF)<br />
wlan.connect('&lt;Network SSID&gt;', '&lt;Network Password&gt;')<br />
url = '<a href="https://routes.googleapis.com/directions/v2:computeRoutes?key=" target="_blank" rel="noopener noreferrer nofollow ugc">https://routes.googleapis.com/directions/v2:computeRoutes?key=</a>&lt;google maps api key&gt;'</p>
<p dir="auto">def loop():<br />
global label0, rect0, label1, http_req, wlan, url, timeseconds, responsejson, sliced<br />
M5.update()<br />
if BtnA.isPressed():<br />
getETA()</p>
<p dir="auto">if <strong>name</strong> == '<strong>main</strong>':<br />
try:<br />
setup()<br />
while True:<br />
loop()<br />
except (Exception, KeyboardInterrupt) as e:<br />
try:<br />
from utility import print_error_msg<br />
print_error_msg(e)<br />
except ImportError:<br />
print("please update to latest firmware")</p>
]]></description><link>https://community.m5stack.com/post/28606</link><guid isPermaLink="true">https://community.m5stack.com/post/28606</guid><dc:creator><![CDATA[JensH]]></dc:creator><pubDate>Sat, 22 Mar 2025 13:49:53 GMT</pubDate></item></channel></rss>