Nearby and Text Search Zero Results - python-3.x

I am trying to get via Python the plain text result from the gmaps / places API. There is a Google Python Libary but wanted to use the real API via a normel requests or urllib call.
import requests
import json
import urllib3
#tried:
sUrl1 = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=kindergarten&location=50.73464,12.52528&radius=50000&fields=formatted_address,name&key=[myapikey]'
sUrl2 = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=kindergarten&location=50.73464'+ r',' + '12.52528&radius=50000&fields=formatted_address'+ r',' + 'name&key=[myapikey]'
sUrl3 = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=kindergarten&location=50.73464'+ r',' + '12.52528&radius=50000&fields=formatted_address'+ r',' + 'name&key=[myapikey]&verify=false'
sUrl4 = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=kindergarten&location=50.73464;12.52528&radius=50000&fields=formatted_address;name&key=[myapikey]'
print (requests.get(sUrl1).text)
print (requests.get(sUrl2).text)
print (requests.get(sUrl3).text)
print (requests.get(sUrl4).text)
Did the same for urllib3, and tried to give it as params but always get this:
{
"html_attributions" : [],
"results" : [],
"status" : "ZERO_RESULTS"
}
When i click on the links (print (sUrl1 or sUrl2 or sUrl3 or sUrl4) ) - I get a wonderful result.
In my API Key there is a "-" but if this is the problem?
Do i need some more headers?
What is the problem?
Can you help?
Thanks

If you add headers it works!
Code for Textsearch - works fine - needed to add headers
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'de,en-US;q=0.7,en;q=0.3',
'Connection': 'keep-alive' ,
'Host': 'maps.googleapis.com',
'Referer': 'maps.googleapis.com',
'TE': 'Trailers',
'Upgrade-Insecure-Requests': '1'
}
urlparams = {
u'query': u'xxx Berlin XXX',
u'location': str(lat)+","+str(long),
u'radius': u'10',
u'fields': u'place_id',
u'key': u'[mySecretKey]'
}
response = requests.get('https://maps.googleapis.com/maps/api/place/textsearch/json', params=urlparams,headers=headers)
myjson = json.loads(response.text)
print (myjson)
place_id = myjson['results'][0]['place_id']

Related

Elasticsearch "register repo" command returns 500 error code but syntax matches the doc (I think)

I have this Python code to register a Google Cloud Storage (GCS) repository:
import requests
from grabconfig import grabconfig
(HOSTS, ign) = grabconfig()
reqHeaders = {'content-type' : 'application/json'}
for h in HOSTS:
url = f'http://{h}:9200'
r = requests.put(f'{url}/_snapshot/prod_backup2',
'''{ \"type\" : \"gcs\" }, { \"settings\" : { \"client\" : \"secondary\", \"bucket\" : \"prod_backup2\" },
{ \"compress\" : \"true\" }}''',
headers=reqHeaders)
print(r)
print(r.json())
r2 = requests.get(f'{url}/_cat/snapshots')
print(r2)
print(r2.json())
The configuration file I am using is the prod.py one:
HOSTS = ['10.x.x.x']
BACKUP_REPO = ['prod_backup2']
But when I run the code I get this error, always:
<Response [500]>
{'error': {'root_cause': [{'type': 'repository_exception', 'reason': '[prod_backup2] repository type [gcs] does not exist'}], 'type': 'repository_exception', 'reason': '[prod_backup2] repository type [gcs] does not exist'}, 'status': 500}
I think I found it: the gcs plugin was not installed on the server I was targeting.
That's supposed to be fixed by Monday, so I'm on to the next task.

Influxdb2 Python API: Path not found

I have a working InfluxDb2 server and, on a Raspberry Pi, the Python client library.
I've generated the the tokens in the server UI and copied an all-areas one into the Python. The test bucket is set up in the UI too. In the Python program I have this:
bucket = "test"
org = "test-org"
#
token = "blabla=="
# Store the URL of your InfluxDB instance
url="http://10.0.1.1:8086/api/v2"
client = influxdb_client.InfluxDBClient(
url=url,
token=token,
org=org
)
Followed later by:
p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
write_api = client.write_api(write_options=SYNCHRONOUS)
write_api.write(bucket='test', org='test-org', record=p)
I've overcome the not-authorized but now, whatever I do, I end up with this:
influxdb_client.rest.ApiException: (404)
Reason: Not Found
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json; charset=utf-8', 'X-Influxdb-Build': 'OSS', 'X-Influxdb-Version': 'v2.2.0', 'X-Platform-Error-Code': 'not found', 'Date': 'Tue, 26 Apr 2022 14:35:50 GMT', 'Content-Length': '54'})
HTTP response body: {
"code": "not found",
"message": "path not found"
}
I've also gone back to Curl which gives me not authorized problem with the same parameters. Any help appreciated, beginning to regret trying to upgrade now.
You don't need the /api/v2 in your url parameter, just url="http://10.0.1.1:8086"
See https://github.com/influxdata/influxdb-client-python#getting-started

How to run REST API to build trigger in google cloud build

I have written a python script in my local machine and trying to run it and getting below error:
Error
{'error': {'code': 400,
'details': [{'#type': 'type.googleapis.com/google.rpc.Help',
'links': [{'description': 'Google developer console '
'API key',
'url': 'https://console.developers.google.com/project/[project_id]/apiui/credential'}]}],
'message': 'The API Key and the authentication credential are from '
'different projects.',
'status': 'INVALID_ARGUMENT'}}
python script to Build trigger
bashCommand = "gcloud auth print-access-token"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
if error:
print(error)
headers = {
'Authorization' : 'Bearer '+str(output)[2:-3],
'Accept' : 'application/json',
'Content-Type' : 'application/json'
}
cloudbuild = {"build":
{"source":
{"repoSource":
{"projectId":"[PROJECT_ID]",
"repoName":"[repoName]",
"branchName":".*"
}
}
},
"description":"API TRigger for all branch",
"name":"[TRIGGER NAME]"
}
data = json.dumps(cloudbuild)
response = requests.post('https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/triggers?key=[API KEY]', headers=headers, data=data)
results_output = response.json()
pprint(results_output)
I also set the project in my local machine
gcloud config set project [project-name]
please give some solution for this.
Thanks in advance.
I removed API Key from request
Access-token is enough to run the above python script

Convert curl command into a working script

I am using an API from check-host.net to ping an website.
My issue is right now that I have no ideea how I could transform the curl command api into an working python script. I tried different approaches which I found on here but sadly none has give me the ouput I am looking for.
Working curl command:
curl -H "Accept: application/json" \ https://check-host.net/check-tcp?host=smtp://gmail.com&max_nodes=1
the respons looks something like that:
{ "ok": 1, "request_id": "29", "permanent_link":
"https://check-host.net/check-report/29", "nodes": {
"7f000001": ["it","Italy", "Marco"] } }
You have to send a Accept: application/json header in your request. You can also use the builtin json decoder in requests.
import requests
headers={
'Accept': 'application/json'
}
r=requests.get('https://check-host.net/check-tcp?host=smtp://gmail.com&max_nodes=1',headers=headers)
print(r.json())
Output
{'nodes': {'us2.node.check-host.net': ['us', 'USA', 'New Jersey', '199.38.245.218', 'AS394625', 'OK']}, 'ok': 1, 'permanent_link': 'https://check-host.net/check-report/a462c3ck399', 'request_id': 'a462c3ck399'}

Reqwest's Client.post() returning 400 bad request for File.io API

I'm learning Rust, and thought it would be handy to build a CLI to share files with the File.io API.
To do so, I am trying to use reqwest to send a request as described in the File.io docs:
# from file.io doc -> works fine
$ curl --data "text=this is a secret pw" https://file.io
> {"success":true,"key":"zX0Vko","link":"https://file.io/zX0Vko","expiry":"14 days"}
When I run the below code, I get a 400 response. Perhaps there's an issue with the headers? I've tried looking at the curl docs to find out what I could be missing, but I'm stumped.
Any help would be appreciated.
My code:
extern crate reqwest;
fn main() {
let client = reqwest::Client::new();
let res = client.post("https://file.io/")
.body("text=this is a practice run")
.send();
println!("{:?}", res);
}
Expected Response:
{"success":true,"key":"SOME_KEY","link":"SOME_LINK","expiry":"14 days"}
Actual Response:
Ok(Response { url: "https://file.io/", status: 400, headers: {"date": "Wed, 06 Feb 2019 03:40:35 GMT", "content-type": "application/json; charset=utf-8", "content-length": "64", "connection": "keep-alive", "x-powered-by": "Express", "x-ratelimit-limit": "5", "x-ratelimit-remaining": "4", "access-control-allow-origin": "*", "access-control-allow-headers": "Cache-Control,X-reqed-With,x-requested-with", "etag": "W/\"40-SEaBd3tIA9c06hg3p17dhWTvFz0\""} })
Your requests are not equivalent. curl --data means you're trying to send a HTML form with content type "x-www-form-urlencoded" or similar, whereas this line in your code
.body("text=this is a practice run")
means "just a text". You should use ReqwestBuilder::form as described here

Resources