如何解决在 Python 中调用 Google Map API 时 SSL 证书验证错误
我正在使用公司笔记本电脑提取 Jupyter Notebook 中一组位置的纬度/经度(系统配置:Windows 10-64bit,Python 3.8.5,pip 版本:20.3.3)- 以下是代码片段我正在使用:
from googlemaps import Client as GoogleMaps
import pandas as pd
gmaps = GoogleMaps(key=xxxxx)
result = gmaps.geocode(address)
首先,我运行以下命令来禁用 SSL 验证(在之前的讨论中建议):
import ssl
try:
_create_unverified_https_context =
ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context =
_create_unverified_https_context
此外,根据此处的讨论,我将 certifi 模块降级到 2015.04.28 版本:https://github.com/googlemaps/google-maps-services-python/issues/191。这些都没有奏效,即使对于单个位置,我也不断收到以下 SSL 验证错误(相同的代码在我的个人系统上运行良好)- 我很感激任何反馈。
SSLCertVerificationError Traceback (most recent call last)
C:\admin_required\Anaconda\lib\site-packages\urllib3\connectionpool.py in urlopen(self,method,url,body,headers,retries,redirect,assert_same_host,timeout,pool_timeout,release_conn,chunked,body_pos,**response_kw)
669 # Make the request on the httplib connection object.
--> 670 httplib_response = self._make_request(
671 conn,C:\admin_required\Anaconda\lib\site-packages\urllib3\connectionpool.py in _make_request(self,conn,**httplib_request_kw)
380 try:
--> 381 self._validate_conn(conn)
382 except (SocketTimeout,BaseSSLError) as e:
C:\admin_required\Anaconda\lib\site-packages\urllib3\connectionpool.py in _validate_conn(self,conn)
977 if not getattr(conn,"sock",None): # AppEngine might not have `.sock`
--> 978 conn.connect()
979
C:\admin_required\Anaconda\lib\site-packages\urllib3\connection.py in connect(self)
361
--> 362 self.sock = ssl_wrap_socket(
363 sock=conn,C:\admin_required\Anaconda\lib\site-packages\urllib3\util\ssl_.py in ssl_wrap_socket(sock,keyfile,certfile,cert_reqs,ca_certs,server_hostname,ssl_version,ciphers,ssl_context,ca_cert_dir,key_password,ca_cert_data)
385 if HAS_SNI and server_hostname is not None:
--> 386 return context.wrap_socket(sock,server_hostname=server_hostname)
387
C:\admin_required\Anaconda\lib\ssl.py in wrap_socket(self,sock,server_side,do_handshake_on_connect,suppress_ragged_eofs,session)
499 # ctx._wrap_socket()
--> 500 return self.sslsocket_class._create(
501 sock=sock,C:\admin_required\Anaconda\lib\ssl.py in _create(cls,context,session)
1039 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
-> 1040 self.do_handshake()
1041 except (OSError,ValueError):
C:\admin_required\Anaconda\lib\ssl.py in do_handshake(self,block)
1308 self.settimeout(None)
-> 1309 self._sslobj.do_handshake()
1310 finally:
SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)
During handling of the above exception,another exception occurred:
MaxRetryError Traceback (most recent call last)
C:\admin_required\Anaconda\lib\site-packages\requests\adapters.py in send(self,request,stream,verify,cert,proxies)
438 if not chunked:
--> 439 resp = conn.urlopen(
440 method=request.method,C:\admin_required\Anaconda\lib\site-packages\urllib3\connectionpool.py in urlopen(self,**response_kw)
725
--> 726 retries = retries.increment(
727 method,error=e,_pool=self,_stacktrace=sys.exc_info()[2]
C:\admin_required\Anaconda\lib\site-packages\urllib3\util\retry.py in increment(self,response,error,_pool,_stacktrace)
445 if new_retry.is_exhausted():
--> 446 raise MaxRetryError(_pool,error or ResponseError(cause))
447
MaxRetryError: HTTPSConnectionPool(host='maps.googleapis.com',port=443): Max retries exceeded with url: /maps/api/geocode/json?address=K1Z+5J3&key=AIzaSyAG1zs2SEQvdlOFLdzXv2AHrZmvdQfRcSo (Caused by SSLError(SSLCertVerificationError(1,'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)')))
During handling of the above exception,another exception occurred:
SSLError Traceback (most recent call last)
C:\admin_required\Anaconda\lib\site-packages\googlemaps\client.py in _request(self,params,first_request_time,retry_counter,base_url,accepts_clientid,extract_body,requests_kwargs,post_json)
227 try:
--> 228 response = requests_method(base_url + authed_url,229 **final_requests_kwargs)
C:\admin_required\Anaconda\lib\site-packages\requests\sessions.py in get(self,**kwargs)
542 kwargs.setdefault('allow_redirects',True)
--> 543 return self.request('GET',**kwargs)
544
C:\admin_required\Anaconda\lib\site-packages\requests\sessions.py in request(self,data,cookies,files,auth,allow_redirects,proxies,hooks,json)
529 send_kwargs.update(settings)
--> 530 resp = self.send(prep,**send_kwargs)
531
C:\admin_required\Anaconda\lib\site-packages\requests\sessions.py in send(self,**kwargs)
642 # Send the request
--> 643 r = adapter.send(request,**kwargs)
644
C:\admin_required\Anaconda\lib\site-packages\requests\adapters.py in send(self,proxies)
513 # This branch is for urllib3 v1.22 and later.
--> 514 raise SSLError(e,request=request)
515
SSLError: HTTPSConnectionPool(host='maps.googleapis.com',another exception occurred:
TransportError Traceback (most recent call last)
<ipython-input-14-1e4153e9ede0> in <module>
5
6 # Geocoding an address
----> 7 geocode_result = gmaps.geocode(addresses['MA_POSTAL_CD'][0])
8 #addresses['lat'][x] = geocode_result[0]['geometry']['location'] ['lat']
9 #addresses['long'][x] = geocode_result[0]['geometry']['location']['lng']
C:\admin_required\Anaconda\lib\site-packages\googlemaps\client.py in wrapper(*args,**kwargs)
354 def wrapper(*args,**kwargs):
355 args[0]._extra_params = kwargs.pop("extra_params",None)
--> 356 result = func(*args,**kwargs)
357 try:
358 del args[0]._extra_params
C:\admin_required\Anaconda\lib\site-packages\googlemaps\geocoding.py in geocode(client,address,components,bounds,region,language)
66 params["language"] = language
67
---> 68 return client._request("/maps/api/geocode/json",params)["results"]
69
70
C:\admin_required\Anaconda\lib\site-packages\googlemaps\client.py in _request(self,post_json)
231 raise googlemaps.exceptions.Timeout()
232 except Exception as e:
--> 233 raise googlemaps.exceptions.TransportError(e)
234
235 if response.status_code in _RETRIABLE_STATUSES:
TransportError: HTTPSConnectionPool(host='maps.googleapis.com','[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)')))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。