使用PyAPNS发送Push消息时的ssl.SSLError

时间:2014-05-04 14:20:19

标签: python certificate apple-push-notifications pyapns

我一直在尝试访问APNS,以便向我开发的iOS应用发送推送通知。我已经创建了开发和生产证书请求,并在Apple开发中心签名:

我的证书

enter image description here

对于每个证书(生产和开发),我有三个文件:

  1. aps_development.cer: A。cer`文件我在签署证书申请后已从Apple下载。
  2. dev.pem:来自OSX钥匙串的.pem公钥文件。
  3. dev.p12:来自OSX钥匙串的.p12私钥文件,带有密码(和dev_nopass.p12:来自OSX钥匙串的.p12私钥文件,带密码)
  4. 我尝试了什么

    1. 克隆并安装PyAPNs表单github(PyPi版本似乎有点旧并导致导入错误。)。
    2. 将示例代码修改为:
    3. 来自apns导入APN,Frame,Payload

      p12 = '/path/to/dev.p12'
      p12_nopass = '/path/to/dev_nopass.p12'
      pem = '/path/to/dev.pem'
      cer = '/path/to/aps_development.cer'
      
      apns = APNs(use_sandbox=True,
                  cert_file=pem,
                  key_file=p12_nopass)
      
      # Send a notification
      # Dummy token, but it does not cause the error (SSL fails before token check)
      token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87'
      payload = Payload(alert="Hello World!", sound="default", badge=1)
      apns.gateway_server.send_notification(token_hex, payload)
      

      结果:

      Traceback (most recent call last):
        File "apple_push_driver.py", line 18, in <module>
          apns.gateway_server.send_notification(token_hex, payload)
        File "/path_to/site-packages/apns.py", line 381, in send_notification
          self.write(self._get_notification(token_hex, payload))
        File "/path_to/apns.py", line 174, in write
          return self._connection().write(string)
        File "/path_to/apns.py", line 167, in _connection
          self._connect()
        File "/path_to/apns.py", line 151, in _connect
          self._ssl = wrap_socket(self._socket, self.key_file, self.cert_file)
        File "/path_to/ssl.py", line 387, in wrap_socket
          ciphers=ciphers)
        File "/path_to/ssl.py", line 141, in __init__
          ciphers)
      ssl.SSLError: [Errno 336265225] _ssl.c:351: error:140B0009:SSL routines:SSL_CTX_use_PrivateKey_file:PEM lib
      

      我尝试了p12p12_nopass,生产和开发,并获得了相同的结果。

      知道为什么我无法建立SSL连接以通过PyAPN发送推送通知吗?

1 个答案:

答案 0 :(得分:0)

问题在于使用原始.p12 / cer。文件。应使用精确说明here将它们转换为pem文件。