【问题标题】:ssl.SSLError when sending Push message using PyAPNS使用 PyAPNS 发送推送消息时出现 ssl.SSLError
【发布时间】:2014-05-04 14:20:18
【问题描述】:

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

我的证书

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

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

我尝试过的

  1. 从 github 克隆并安装 PyAPNs(PyPi 版本似乎有点旧,导致导入错误。)。
  2. 将示例代码改编为:

从 apns 导入 APNs、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 连接以通过 PyAPNs 发送推送通知吗?

【问题讨论】:

    标签: python certificate apple-push-notifications pyapns


    【解决方案1】:

    问题在于使用原始的.p12 / cer。文件。应该将它们转换为pem 文件,使用精细指令here

    【讨论】:

    • 您能更具体地了解证书吗?那么certificate_filekey_file 使用的是哪个?
    • 最终对两者使用相同的 pem 文件。工作。
    • 请您在这里评论我的相关问题:stackoverflow.com/questions/42350151/…
    猜你喜欢
    • 2023-03-29
    • 2023-03-26
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 2013-08-22
    • 2016-12-14
    • 2016-12-22
    • 2020-12-05
    相关资源
    最近更新 更多