Apple 推送通知 - “无法加载客户端密钥:-8178”

时间:2021-03-22 20:47:31

标签: curl centos apple-push-notifications http2 pem

我目前正在尝试使用 PHP cURL 连接到 CentOS 生产服务器上的苹果推送通知服务来发送推送通知。我能够在具有相同库的单独开发服务器上成功发送通知(它是生产服务器的克隆),但是当我将目标 url 从 https://api.sandbox.push.apple.com/3/device 切换到 https://api.push.apple.com/3/device/ 时,我得到了这个cUrl 错误:无法加载客户端密钥:-8178 (SEC_ERROR_BAD_KEY)

我的服务器设置在两台服务器之间是相同的,这是我的 curl 和 openssl 信息:

curl --version
curl 7.61.0 (x86_64-redhat-linux-gnu) libcurl/7.61.0 NSS/3.36 zlib/1.2.7 libpsl/0.7.0 (+libicu/50.1.2) libssh2/1.8.0 nghttp2/1.33.0
Release-Date: 2018-07-11
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz HTTP2 UnixSockets HTTPS-proxy PSL Metalink

和openssl:

openssl version
OpenSSL 1.0.2k-fips

据我所知,这些是使用 HTTP2 和访问 APN 可接受的设置。这是我用于发送通知的 php 代码:

// these are loaded from a config file. I know these are correct because it works on the development server
private static $passphrase = CERTIFICATE_PASSWORD; //password for pem file
private static $certificate_file = CERTIFICATE_FILE; // pem file
private static $apns_topic = APNS_TOPIC; // app bundle id
private static $ios_url = IOS_URL; // "https://api.push.apple.com/3/device/"
...
public static function sendNotification($data, $devicetoken){
    $wsTitle = $data['mtitle'];
    $wsBody = $data['mdesc'];
    $body['aps'] = array(
        'alert' => array(
            'title' => $wsTitle,
            'body' => $wsBody,
        ),
        'sound' => 'default'
    );
    $payload = json_encode($body);
    $apns_topic = self::$apns_topic;
    $url = self::$ios_url . $devicetoken;    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_HTTP_VERSION,3);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["apns-topic: $apns_topic"]);
    curl_setopt($ch, CURLOPT_SSLCERT, self::$certificate_file); //pem file
    curl_setopt($ch, CURLOPT_SSLCERTPASSWD, self::$passphrase); //pem secret
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $response = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo curl_error($ch);
 }

上述脚本的输出是之前讨论过的这个错误:无法加载客户端密钥:-8178 (SEC_ERROR_BAD_KEY)。 $response 为空,$httpcode 为 0,这意味着据我所知,这根本无法连接到 Apple。

尝试向 Apple 的生产 URL 发送推送通知似乎有我不知道的额外安全要求。同样,这个脚本可以在相同的服务器上完美运行,只需使用沙盒苹果 url。

有人对我在这里可能遗漏的东西有任何想法吗?任何帮助表示赞赏。

0 个答案:

没有答案