Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

SSL Certificate Verification Error Upon Calling Google Map API in Python

I am using a corporate laptop to extract latitudes/longitudes for a set of locations in Jupyter Notebook (system config: Windows 10-64bit , Python 3.8.5, pip version: 20.3.3 )- Below is the code snippet that I am using:

from googlemaps import Client as GoogleMaps
import pandas as pd 
gmaps = GoogleMaps(key=xxxxx)
result = gmaps.geocode(address)

First, I run the following to disable SSL verification (which was suggested in a previous discussion):

import ssl 
try:
    _create_unverified_https_context = 
    ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = 
    _create_unverified_https_context

Also, I downgraded certifi module to 2015.04.28 version following the discussion here: https://github.com/googlemaps/google-maps-services-python/issues/191. None of these worked and I keep getting the following SSL verification error even for a single location (same code works fine on my personal system)- I'd appreciate any feedback.

SSLCertVerificationError                  Traceback (most recent call last)
C:admin_requiredAnacondalibsite-packagesurllib3connectionpool.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_requiredAnacondalibsite-packagesurllib3connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    380         try:
--> 381             self._validate_conn(conn)
    382         except (SocketTimeout, BaseSSLError) as e:

C:admin_requiredAnacondalibsite-packagesurllib3connectionpool.py in _validate_conn(self, conn)
    977         if not getattr(conn, "sock", None):  # AppEngine might not have  `.sock`
--> 978             conn.connect()
    979 

C:admin_requiredAnacondalibsite-packagesurllib3connection.py in connect(self)
    361 
--> 362         self.sock = ssl_wrap_socket(
    363             sock=conn,

C:admin_requiredAnacondalibsite-packagesurllib3utilssl_.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_requiredAnacondalibssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)
    499         # ctx._wrap_socket()
--> 500         return self.sslsocket_class._create(
    501             sock=sock,

C:admin_requiredAnacondalibssl.py in _create(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, 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_requiredAnacondalibssl.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_requiredAnacondalibsite-packages
equestsadapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    438             if not chunked:
--> 439                 resp = conn.urlopen(
    440                     method=request.method,

C:admin_requiredAnacondalibsite-packagesurllib3connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    725 
--> 726             retries = retries.increment(
    727                 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]

C:admin_requiredAnacondalibsite-packagesurllib3util
etry.py in increment(self, method, url, response, error, _pool, _stacktrace)
    445         if new_retry.is_exhausted():
--> 446             raise MaxRetryError(_pool, url, 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_requiredAnacondalibsite-packagesgooglemapsclient.py in _request(self, url, 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_requiredAnacondalibsite-packages
equestssessions.py in get(self, url, **kwargs)
    542         kwargs.setdefault('allow_redirects', True)
--> 543         return self.request('GET', url, **kwargs)
    544 

C:admin_requiredAnacondalibsite-packages
equestssessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    529         send_kwargs.update(settings)
--> 530         resp = self.send(prep, **send_kwargs)
    531 

C:admin_requiredAnacondalibsite-packages
equestssessions.py in send(self, request, **kwargs)
    642         # Send the request
--> 643         r = adapter.send(request, **kwargs)
    644 

C:admin_requiredAnacondalibsite-packages
equestsadapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    513                 # This branch is for urllib3 v1.22 and later.
--> 514                 raise SSLError(e, request=request)
    515 

SSLError: 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:

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_requiredAnacondalibsite-packagesgooglemapsclient.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_requiredAnacondalibsite-packagesgooglemapsgeocoding.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_requiredAnacondalibsite-packagesgooglemapsclient.py in _request(self, url, params, first_request_time, retry_counter, base_url, accepts_clientid, extract_body, requests_kwargs, 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', 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)')))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...