Python Forum
New iOS token based notification - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: New iOS token based notification (/thread-2288.html)



New iOS token based notification - fbartolom - Mar-04-2017

I am trying to implement the new iOS token based notifications and for this I found a tutorial named: "token-based-authentication-http2-example-apns" that I implemented starting with:
import json
import jwt
import time
from hyper import HTTPConnection
ALGORITHM = 'ES256'
APNS_KEY_ID = 'H6Q3X7RYZG'
APNS_AUTH_KEY = 'APNsAuthKey_H6Q3X7RYZG.p8'
TEAM_ID = 'GF9PQ87F68'
REGISTRATION_ID = '713d3a9f4d932ac824caf9373a2f182b2dfa710fb7b6214e3bd405ae1e32d0aa'
BUNDLE_ID = 'com.meditation.iPujaPro'
f = open(APNS_AUTH_KEY)
secret = f.read()
print(secret)
token = jwt.encode(
    {
        'iss': TEAM_ID,
        'iat': time.time()
    },
    secret, 
    algorithm = ALGORITHM,
    headers={
        'alg': ALGORITHM,
        'kid': APNS_KEY_ID
    }
)
Yet when I run it I get:
Error:
Traceback (most recent call last):   File "simpleNotification.py", line 26, in <module>     'kid': APNS_KEY_ID   File "/usr/lib64/python2.7/site-packages/jwt/api_jwt.py", line 56, in encode     json_payload, key, algorithm, headers, json_encoder   File "/usr/lib64/python2.7/site-packages/jwt/api_jws.py", line 98, in encode     key = alg_obj.prepare_key(key)   File "/usr/lib64/python2.7/site-packages/jwt/algorithms.py", line 226, in prepare_key     key = load_pem_private_key(key, password=None, backend=default_backend())   File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 20, in load_pem_private_key     return backend.load_pem_private_key(data, password)   File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/multibackend.py", line 305, in load_pem_private_key     return b.load_pem_private_key(data, password)   File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1084, in load_pem_private_key     password,   File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1253, in _load_key     self._handle_key_loading_error()   File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1325, in _handle_key_loading_error     raise ValueError("Could not unserialize key data.") ValueError: Could not unserialize key data.
If I remove the part after secret I have no error there and of course later. What might it be the problem or is there an alternative solution to implement the thing?