I have a Django site that works without issue when run on the Django dev server on my local machine. When I try to run the site on my pythonanywhere account, everything works except for a single call to the Google Maps Geolocation API.
The function call that is failing is:
def geolocate():
url = "https://www.googleapis.com/geolocation/v1/geolocate?key=%s" % API_KEY
r = requests.post(url)
lat_lng = r.json()['location']
return (lat_lng['lat'], lat_lng['lng'])
The response that I get back from the requests.post()
call when run on pythonanywhere is:
{
"error": {
"errors": [
{
"domain": "geolocation",
"reason": "invalidRequest",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
Whereas when I run it on the dev server I get:
{
"location": {
"lat": 39.9546734,
"lng": -96.5985613
},
"accuracy": 2678.0
}
which is the response I am looking for.
I have also tried making the request with curl
on both my local machine, and from a pythonanywhere bash console. Using this command:
curl 'https://www.googleapis.com/geolocation/v1/geolocate?key=MY_API_KEY' -H "Content-Type: application/json" -d {}
I get the same 400
response from pythonanywhere, but get the expected location data on my local machine. .googleapis.com
is among the whitelisted domains for pythonanywhere, and my Django site on pythonanywhere makes calls to the Google Maps Geocode and Places APIs in JavaScript and in calls to web services without any issue. It seems to just be an issue with the Geolocate API.
One other potentially useful piece of information: when I use the -v
flag with curl
to get more verbose output, I see that when I make the "Bad Request" on pythonanywhere I get the line:
* Server GSE is not blacklisted
which I do not see when I run the command locally. I have done some searching and cannot figure out what that means or if it is relevant, but it stood out so I thought I would mention in case it raises flags for more knowledgeable users.
I have seen this question, and this question, as well as a number of seemingly related, unresolved bug reports, but I wanted to post my question as well since my question does not seem to be identical to any existing questions I have found. Any help would be appreciated. Thank you!
I did some experimenting on a DigitalOcean droplet, and found that, while the geolocation request successfully returns location information when run on the droplet, it gives me latitude and longitude of an address in New York City (the droplet is on a server that I know to be located in New York City). Unfortunately, since I am trying to geolocate the user, this information is even less useful than an error message...
I still don't know why the request fails on pythonanywhere but succeeds on DigitalOcean (except that maybe, as @Glenn suggests, Goggle knows in one case and not the other that it shouldn't even bother retrieving location information), but I guess that is no longer relevant to my issue, because even if it did succeed on pythonanywhere, it would give me useless - or worse, misleading - information. I guess I will have to figure out another way to get the users location.
Sending an empty request body to google geolocation amounts to "tell me where this machine is". I think that Google is smart enough to know that trying to get the location of an AWS instance is not meaningful.