首页 > 解决方案 > Python 3.7 和 Dataflow - SSL 证书问题

问题描述

我需要使用 google cloud api 来编写我的 Dataflow 作业。

据我了解,我不能使用,pip install google-cloud-dataflow因为 Apache Beam 不能在 Python 3 上工作,所以我一直在使用googleapiclient.discovery. 但是,当我发出build()命令时,它会引用错误来轰炸:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)

背景说明:

这是我的代码:

from oauth2client.client import GoogleCredentials
from googleapiclient.discovery import build

credentials = GoogleCredentials.get_application_default()

dataflow = build('dataflow', 'v1b3', credentials=credentials)

结果:

回溯(最后一次调用):文件“test_dataflow_creds.py”,第 6 行,在 dataflow = build('dataflow', 'v1b3', credentials=credentials) 文件“/Users/user/.pyenv/versions/unit-test -3.7/lib/python3.7/site-packages/googleapiclient/_helpers.py”,第 130 行,在 positional_wrapper 中返回已包装(*args,**kwargs)文件“/Users/user/.pyenv/versions/unit-test -3.7/lib/python3.7/site-packages/googleapiclient/discovery.py”,第 222 行,在 build requested_url、discovery_http、cache_discovery、cache)文件“/Users/user/.pyenv/versions/unit-test-3.7 /lib/python3.7/site-packages/googleapiclient/discovery.py”,第 269 行,在 _retrieve_discovery_doc 中,内容 = http.request(actual_url) 文件“/Users/user/.pyenv/versions/unit-test-3.7 /lib/python3.7/站点包/httplib2/初始化.py”,第 1924 行,在请求缓存键中,文件“/Users/user/.pyenv/versions/unit-test-3.7/lib/python3.7/site-packages/httplib2/init .py ”,第 1595 行,在_request conn、request_uri、方法、正文、标头文件“/Users/user/.pyenv/versions/unit-test-3.7/lib/python3.7/site-packages/httplib2/init .py ”,第 1501 行,在 _conn_request conn.connect() 文件“ /Users/user/.pyenv/versions/unit-test-3.7/lib/python3.7/site-packages/httplib2/init.py”,第 1291 行,在连接 self.sock = self._context.wrap_socket(sock, server_hostname=self.host) 文件“/Users/user/.pyenv/versions/3.7.0/lib/python3.7/ssl .py”,第 412 行,在 wrap_socket session=session 文件中“/Users/user/.pyenv/versions/3.7.0/lib/python3.7/ssl.py”,第 850 行,在 _create self.do_handshake() 文件中“/Users/user/.pyenv/versions/3.7.0/lib/python3.7/ssl.py”,第 1108 行,在 do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败: 无法获取本地颁发者证书 (_ssl.c:1045)

标签: python-3.xgoogle-cloud-dataflow

解决方案


tl;dr:通过将所有证书导出到一个公共文件,然后附加到证书文件中指定的路径中的证书文件来使其工作

脚步:

  1. 在 Firefox > Preferences > View Certificates > Your Certificates 中,导出所有必需的。
  2. 将上述所有 .crt 文件连接成一个大包。
  3. 在 bash 中,运行 python -m requests.certs 以获取 python 正在使用的证书文件。
  4. 将上述步骤 2 中的捆绑证书附加到步骤 3 中的文件中。
  5. 完毕

推荐阅读