python google geolocation api using wifi mac


python google geolocation api using wifi mac



I'm trying to use Google's API for geolocation giving wifi data to determine location. This is their intro. And this is my code


@author: Keith
"""

import requests

payload = {
"considerIp": "false",
"wifiAccessPoints": [
{
"macAddress": "00:25:9c:cf:1c:ac",
"signalStrength": -43,
"signalToNoiseRatio": 0
},
{
"macAddress": "00:25:9c:cf:1c:ad",
"signalStrength": -55,
"signalToNoiseRatio": 0
}
],
'key':'<MyAPIKey>'
}
r = requests.post('https://www.googleapis.com/geolocation/v1/geolocate',
params=payload)
print(r.text)



This is the output


{
"location": {
"lat": 32.3643098,
"lng": -88.703656
},
"accuracy": 6061.0
}



The request ignored all of the payload except the key portion and just found the geolocation using my IP address. So I'm sending the json payload incorrectly. I know this is probably really simple, but I'm stuck and couldn't find an example of python being used with requests to do this type of API query. Thanks



Edit:
Picked up the cURL library and executed this command with success:


curl -d @your_filename.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=<myapikey>"



and got the output I expected. I just want to be able to do the same thing in requests, but the data I'm trying to send is in "your_filename.json".




2 Answers
2



Please try the following:


r = requests.post('https://www.googleapis.com/geolocation/v1/geolocate?key='+ '<MyAPIKey>', json=payload)



Note the key was moved to query params (URL) and json argument was used in place of params.


json


params





Using json instead of data for param fixed it. Thanks
– Keith Wilcox
Jul 2 at 10:52



Okay I figured out the error of my ways. Taking a better look at requests.post function, I see that I'm not using the parameters argument correctly. After this, it worked perfectly,


r = requests.post('https://www.googleapis.com/geolocation/v1/geolocate', params=parameters, json=mydata, headers=headers)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages