python - 对 Google Geocoding API 的普通 HTTP API 调用因 Python 请求模块而失败

标签 python google-maps ssl google-geocoding-api

我在公司代理中,所以我经常遇到 SSL 问题并且不得不回退到纯 HTTP(当它不是涉及敏感数据的问题时)。因此,我正在尝试使用 Google's Geocoding API 进行地理标记通过纯 HTTP。当我创建一个调用并在终端上使用 curl 执行它时,我得到了预期的 JSON 响应。但是,当我将相同的 URL 放入 Python 脚本并使用 requests.get 访问该 URL 时,我收到 SSL 错误:

{u'status': u'REQUEST_DENIED', u'error_message': u'Requests to this API must be over SSL.', u'results': []}

Python 非常简单,但这里是为了后代:

import json
import requests

response = requests.get('http://maps.googleapis.com/maps/api/geocode/json?address=some+address+here&key=my-key')
json_data = json.loads(response.text)

print json_data

当然,如果我尝试使用 HTTPS 调用,我会遇到代理证书错误。有什么想法吗?


更新:

我知道我可以将 verify=false 标志添加到 requests 方法调用来解决 SSL 证书问题,但这并不能帮助我理解调用的原因即使它表面上接受纯 HTTP 调用,也会从 API 反弹。

最佳答案

您应该为您的请求使用 https 协议(protocol)

import json
import requests

response = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address=some+address+here&key=my-key')
json_data = json.loads(response.text)

print json_data

示例 url 来自 Google documentation : https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=my_key

给予

{u'status': u'OK', u'results': [{u'geometry': {u'location': {u'lat': 37.422245, u'lng': -122.0840084}, u'viewport': {u'northeast': {u'lat': 37.42359398029149, u'lng': -122.0826594197085}, u'southwest': {u'lat': 37.4208960197085, u'lng': -122.0853573802915}}, u'location_type': u'ROOFTOP'}, u'address_components': [{u'long_name': u'1600', u'types': [u'street_number'], u'short_name': u'1600'}, {u'long_name': u'Amphitheatre Parkway', u'types': [u'route'], u'short_name': u'Amphitheatre Pkwy'}, {u'long_name': u'Mountain View', u'types': [u'locality', u'political'], u'short_name': u'Mountain View'}, {u'long_name': u'Santa Clara County', u'types': [u'administrative_area_level_2', u'political'], u'short_name': u'Santa Clara County'}, {u'long_name': u'California', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'CA'}, {u'long_name': u'United States', u'types': [u'country', u'political'], u'short_name': u'US'}, {u'long_name': u'94043', u'types': [u'postal_code'], u'short_name': u'94043'}], u'place_id': u'ChIJ2eUgeAK6j4ARbn5u_wAGqWA', u'formatted_address': u'1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA', u'types': [u'street_address']}]}

关于python - 对 Google Geocoding API 的普通 HTTP API 调用因 Python 请求模块而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32785480/

相关文章:

google-maps - 动态加载Google Maps api

ios - Apple 从当前位置映射路由请求 URL

postgresql - 如何在谷歌地图上绘制 Postgis Shortest_path 线?

java - 通过 TLS 实现 LDAP

node.js - NodeJS 和 TLS,获取客户端证书

Python:创建一个 month() 函数以将 1 - 12 作为输入并返回相应的月份

Python:next() 无法识别

python -> 替换转义字符以将十六进制字符串从 0x 转换为\x

python - pypi中找不到包

r - R 中 twitteR 的 SSL 证书失败