python google geolocation api using wifi mac

2024/11/15 7:02:08

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 requestspayload =         {"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".

Answer

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.

https://en.xdnf.cn/q/119445.html

Related Q&A

Python requests and variable payload

Reticulated members,I am attempting to use a GET method that is supported against the endpoint. However, I am using python and wanting to pass the user raw_input that is assigned to a variable:uid = ra…

How should I read and write a configuration file for TkInter?

Ive gathered numbers in a configuration file, and I would like to apply them to buttons. Clicking the button should allow the number to be changed and then re-written to the config file. My current cod…

How to convert this nested dictionary into one single dictionary in Python 3? [duplicate]

This question already has answers here:Convert nested dictionary into a dictionary(2 answers)Flatten nested dictionaries, compressing keys(32 answers)Closed 4 years ago.I have a dictionary like this:a …

How to click unopened tabs where the numbers change

How do I click all the unopened tabs pages where the value changes when you click tabs? (see image below)Take the following script, based off of this question with the following approach:clickMe = wai…

Xlwings / open password protected worksheet in xlsx?

I get an answer how to open a password protected xlsx with xlwings: Xlwings / open password protected xlsx? wb = xlwings.Book("file.xlsx", password="Passw0rd!")But can i also open …

Wrapping an opencv implementaion of an error level analysis algorithm using cython

i have implemented an error level analysis algorithm using c++(opencv version 2.4) and i want to build a python wrapper for it using cython. I have read some part of the documentation of cython for c++…

Setting yticks Location Matplotlib

Im trying to create a plot which has y axis exactly same with this : And Im in this situation and everything is ok until this point:When i try this lines:ax.set_yticks(range(20,67,10)) ax.set_yticklabe…

How to merge list to become string without adding any character in python?

I found that I can join them with -.join(name) but I dont want to add any character. Lets say I have [stanje1, |, st6, , stanje2, |, #] and I want to be like thisstanje1|st6,stanje2|#

Removing a key from a nested dictionary based on value

I previusly asked about adding, and someone helped me out with append. My new problem is trying to delete a key with a nested list, e.g.:JSON:data = {"result":[{"name":"Teddy&q…

Is dot product and normal multiplication results of 2 numpy arrays same?

I am working with kernel PCA in Python and I have to find the values after projecting the original data to the principal components.I use the equation fv = eigvecs[:,:ncomp]print(len(fv))td = fv.T …