Python 3.7 和 Dataflow - SSL 证书问题

Python 3.7 and Dataflow - SSL Certificate Issue

我需要使用 google 云 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','v1b3',凭据=凭据) 文件“/Users/user/.pyenv/versions/unit-test-3.7/lib/python3.7/site-packages/googleapiclient/_helpers.py”,第 130 行,在 positional_wrapper 中 return 包装(*args,**kwargs) 文件“/Users/user/.pyenv/versions/unit-test-3.7/lib/python3.7/site-packages/googleapiclient/discovery.py”,第 222 行,在构建中 requested_url、discovery_http、cache_discovery、缓存) 文件“/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/site-packages/httplib2/init.py”,第 1924 行,位于要求 缓存键, 文件“/Users/user/.pyenv/versions/unit-test-3.7/lib/python3.7/site-packages/httplib2/init.py”,第 1595 行,位于_要求 康恩,request_uri,方法,body,headers 文件“/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(袜子, 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)

tl;dr:通过将所有证书导出到一个公共文件,然后附加到 Certifi

指定的路径中的证书文件使其工作

步骤:

  1. 在 Firefox > 首选项 > 查看证书 > 您的证书中,导出所有需要的证书。
  2. 将上述所有 .crt 文件合并成一个大包。
  3. 在 bash 中,运行 python -m requests.certs 获取 python 正在使用的证书文件。
  4. 将上面第 2 步中的捆绑证书附加到第 3 步中的文件中。
  5. 完成