How to make googlemaps python library detect SSL certificates?
Question: **The problem**

Make to work:

    
    
    import googlemaps
        gmaps = googlemaps.Client(key='AIza...')
        # Geocoding an address
        geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
    

**Tried so far ...**

I fixed all requests library relevant issues:

    
    
    >>> requests.__version__
    '2.9.1'
    

I have requests installed through pip.

Also I tried to expose the problem by trying another case:

    
    
    import requests
    import certifi
    
    r = requests.get('https://graph.facebook.com/spotify', verify=certifi.where())
    r = requests.get('https://graph.facebook.com/spotify')
    

All works without any problems.

Then:

    
    
    openssl s_client -connect graph.facebook.com:443 -CAfile $(python -m certifi)
    

Works without any problem.

But:

    
    
    import googlemaps
    gmaps = googlemaps.Client(key='AIza...')
    # Geocoding an address
    geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
    

Returns:

    
    
    Traceback (most recent call last):
      File "get_times.py", line 7, in <module>
        geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
      File "/usr/local/lib/python2.7/dist-packages/googlemaps/geocoding.py", line 68, in geocode
        return client._get("/maps/api/geocode/json", params)["results"]
      File "/usr/local/lib/python2.7/dist-packages/googlemaps/client.py", line 205, in _get
        raise googlemaps.exceptions.TransportError(e)
    googlemaps.exceptions.TransportError: bad handshake: Error([('SSL routines', 'SSL3_GET_SERVER_CERTIFICATE', 'certificate verify failed')],)
    

What's double weird - it seems to work this way (I cut out the long hex-
codes):

    
    
    $ openssl s_client -connect maps.googleapis.com:443 -CAfile $(python -m certifi) 
    CONNECTED(00000003)
    depth=3 C = US, O = Equifax, OU = Equifax Secure Certificate Authority
    verify return:1
    depth=2 C = US, O = GeoTrust Inc., CN = GeoTrust Global CA
    verify return:1
    depth=1 C = US, O = Google Inc, CN = Google Internet Authority G2
    verify return:1
    depth=0 C = US, ST = California, L = Mountain View, O = Google Inc, CN = *.googleapis.com
    verify return:1
    ---
    Certificate chain
     0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.googleapis.com
       i:/C=US/O=Google Inc/CN=Google Internet Authority G2
     1 s:/C=US/O=Google Inc/CN=Google Internet Authority G2
       i:/C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
     2 s:/C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
       i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
    ---
    Server certificate
    -----BEGIN CERTIFICATE-----
    ...
    -----END CERTIFICATE-----
    subject=/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.googleapis.com
    issuer=/C=US/O=Google Inc/CN=Google Internet Authority G2
    ---
    No client certificate CA names sent
    ---
    SSL handshake has read 3820 bytes and written 431 bytes
    ---
    New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES128-GCM-SHA256
    Server public key is 2048 bit
    Secure Renegotiation IS supported
    Compression: NONE
    Expansion: NONE
    SSL-Session:
        Protocol  : TLSv1.2
        Cipher    : ECDHE-RSA-AES128-GCM-SHA256
        Session-ID: ...
        Session-ID-ctx: 
        Master-Key: ...
        Key-Arg   : None
        PSK identity: None
        PSK identity hint: None
        SRP username: None
        TLS session ticket lifetime hint: 100800 (seconds)
        TLS session ticket:
        0000 - ...                                     ....
    
        Start Time: 1454060879
        Timeout   : 300 (sec)
        Verify return code: 0 (ok)
    ---
    

No idea if this is a valid approach, but when I tried:

    
    
    import requests
    import certifi
    
    API_KEY = "AIza..."
    url = ("https://maps.googleapis.com/maps/api/directions/json?"
           "origin=75+9th+Ave+New+York,+NY&"
           "destination=MetLife+Stadium+1+MetLife+Stadium+Dr+East+Rutherford,+NJ+07073&"
           "key=%s") % (API_KEY)
    r = requests.get(url, verify=certifi.where())
    

The result was:

    
    
    Traceback (most recent call last):
      File "get_times.py", line 10, in <module>
        r = requests.get(url, verify=certifi.where())
      File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 67, in get
        return request('get', url, params=params, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 53, in request
        return session.request(method=method, url=url, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 468, in request
        resp = self.send(prep, **send_kwargs)
      File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 576, in send
        r = adapter.send(request, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 447, in send
        raise SSLError(e, request=request)
    requests.exceptions.SSLError: bad handshake: Error([('SSL routines', 'SSL3_GET_SERVER_CERTIFICATE', 'certificate verify failed')],)
    

I'm not running this on any server or hosting... I'm just trying to make it
work as a simple CLI script on my PC...


Answer: Thanks to Liam Horne for his answer here:
<http://stackoverflow.com/a/34665344/552621>

> I found a solution. There seems to be a major issue in the version of
> `certifi` that was running. I found this out from this (very long) GitHub
> issue: <https://github.com/certifi/python-certifi/issues/26>
>
> **TL;DR**
>
> `pip uninstall -y certifi && pip install certifi==2015.04.28`

