New iOS based notification script - fbartolom - Apr-14-2017
I am trying to adopt the new Apple APN technology with the following Python script:
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()
token = jwt.encode(
{
'iss': TEAM_ID,
'iat': time.time()
},
secret,
algorithm= ALGORITHM,
headers={
'alg': ALGORITHM,
'kid': APNS_KEY_ID
}
)
path = '/3/device/{0}'.format(REGISTRATION_ID)
request_headers = {
'apns-expiration': '0',
'apns-priority': '10',
'apns-topic': BUNDLE_ID,
'authorization': 'bearer {0}'.format(token.decode('ascii'))
}
# Open a connection the APNS server
conn = HTTPConnection('api.development.push.apple.com:443')
payload_data = {
'aps': { 'alert' : 'All your base are belong to us.' }
}
payload = json.dumps(payload_data).encode('utf-8')
# Send our request
conn.request(
'POST',
path,
payload,
headers=request_headers
)
resp = conn.get_response()
print(resp.status)
print(resp.read())
# If we are sending multiple requests, use the same connection
payload_data = {
'aps': { 'alert' : 'You have no chance to survive. Make your time.' }
}
payload = json.dumps(payload_data).encode('utf-8')
conn.request(
'POST',
path,
payload,
headers=request_headers
)
resp = conn.get_response()
print(resp.status)
print(resp.read()) Yet, when I run it I get the following error: what should I do to have it working?
Output: python3 apns_http2.py
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/jwt/algorithms.py", line 224, in prepare_key
key = load_pem_public_key(key, backend=default_backend())
File "/usr/local/lib/python3.5/site-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key
return backend.load_pem_public_key(data)
File "/usr/local/lib/python3.5/site-packages/cryptography/hazmat/backends/multibackend.py", line 314, in load_pem_public_key
return b.load_pem_public_key(data)
File "/usr/local/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1110, in load_pem_public_key
self._handle_key_loading_error()
File "/usr/local/lib/python3.5/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.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "apns_http2.py", line 25, in <module>
'kid': APNS_KEY_ID
File "/usr/local/lib/python3.5/site-packages/jwt/api_jwt.py", line 56, in encode
json_payload, key, algorithm, headers, json_encoder
File "/usr/local/lib/python3.5/site-packages/jwt/api_jws.py", line 98, in encode
key = alg_obj.prepare_key(key)
File "/usr/local/lib/python3.5/site-packages/jwt/algorithms.py", line 226, in prepare_key
key = load_pem_private_key(key, password=None, backend=default_backend())
File "/usr/local/lib/python3.5/site-packages/cryptography/hazmat/primitives/serialization.py", line 20, in load_pem_private_key
return backend.load_pem_private_key(data, password)
File "/usr/local/lib/python3.5/site-packages/cryptography/hazmat/backends/multibackend.py", line 305, in load_pem_private_key
return b.load_pem_private_key(data, password)
File "/usr/local/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1084, in load_pem_private_key
password,
File "/usr/local/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1253, in _load_key
self._handle_key_loading_error()
File "/usr/local/lib/python3.5/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.
|