带有 SSL 证书的 Django 请求

Django Request with a SSL Certificate

在 django 视图中,我需要向另一个 API 发出 post 请求。 API 要求将 SSL 证书附加到请求中。我有 ssl .crt 文件。

我该如何实现?

我在这里尝试使用这个实现: https://gist.github.com/erikbern/756b1d8df2d1487497d29b90e81f8068

但是即使我 运行 pycharm 作为管理员我也会收到这个错误:

Traceback (most recent call last):
  File "C:\Applications\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Applications\venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Applications\venv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "", line 350, in pge_auth_v1_redirect_view
    with pfx_to_pem(pem_file_path, '') as cert:
  File "c:\program files\python37\Lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "", line 271, in pfx_to_pem
    f_pem = open(t_pem.name, 'wb')
PermissionError: [Errno 13] Permission denied: 'C:\Users\fpier\AppData\Local\Temp\tmpg7i7qtun.pem'

我找到的解决方案是通过连接 public 和私有文件来创建一个 pem 文件。

        req = requests.post(
            url,
            params=params,
            headers=headers,
            auth=client_auth,
            verify=True,
            cert=self._pem_file_path
        )