Search code examples
pythonrestpython-requestscertifi

Python requests.get() raises SSL error accessing web API


I'm trying to access the at https://www.ncdc.noaa.gov/cdo-we/api/v2/. This requires a token one receives immediately upon requesting it here. Then:

import requests


def get_noaa_data(url, data_type, header):

    r = requests.get(url, data_type, headers=header)
    print(r)


if __name__ == '__main__':

    token = 'longalphabetictoken'
    creds = dict(token=token)
    dtype = 'dataset'
    url = 'https://www.ncdc.noaa.gov/cdo-web/api/v2/'

    get_noaa_data(url, dtype, creds)

gives requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:720)

I understand I can set verify=False, but I do want to verify. I also want to be able to distribute this code, so I don't want to work around by modifying my cacert.pem. I've also tried this with certifi imported into script. Is there an os-agnostic way to verify the NOAA site's SSL cert that would work if people download my package?

It seems to me that there would be a secure way to access popular APIs like this without verify=False. Is this my organization's machine's problem?

ssl.get_default_verify_paths() = DefaultVerifyPaths(cafile='/data01/anaconda2/envs/imagenv/ssl/cert.pem', capath=None, openssl_cafile_env='SSL_CERT_FILE', openssl_cafile='/data01/anaconda2/envs/imagenv/ssl/cert.pem', openssl_capath_env='SSL_CERT_DIR', openssl_capath='/data01/anaconda2/envs/imagenv/ssl/certs')

FYI: https sites like amazon, google, etc work fine.


Solution

  • You need to make sure that your organization's SSL certificate is signed by a Certification Authority(CA), otherwise ssl connection won't work.