为DeviceCheck创建Json Web令牌

时间:2018-09-22 06:32:51

标签: python ios apple-push-notifications jwt

当前,我已编写此代码来创建JWT,用于DeviceCheck API的服务器通信。我从苹果那里得到了“无法验证授权令牌”的状态。对如何解决有任何想法吗?

import jwt
import uuid
import time
import json


def generateToken(certificate, keyID, teamID):
    ALGORITHM = 'ES256'
    token = jwt.encode(
    {
        'iss': teamID,
        'iat': time.time()
    },
    certificate,
    algorithm= ALGORITHM,
    headers={
        'alg': ALGORITHM,
        'kid': keyID,
        }
    )
    return token

def generateDeviceCheckApplePayload(device_token):
    millis = int(round(time.time() * 1000))
    payload = {
    "device_token" : device_token,
    "transaction_id" : str(uuid.uuid4()),
    "timestamp" : millis,
    }
    payload = json.dumps(payload).encode('utf-8')
    return payload

def verifyDeviceWithApple(device_token):
    auth_token = generateToken(settings.CERTIFICATE, 
        settings.KEY_ID, settings.TEAM_ID)
    headers = {
        'Authorization': 'Bearer ' + str(auth_token),
    }
    data = generateDeviceCheckApplePayload(device_token)
    response = requests.post('https://' + settings.HOST + '/v1/validate_device_token', headers=headers, data=data)
    print(response.text)
    return True

1 个答案:

答案 0 :(得分:0)

答案是,您需要确保您的Apple ID已经付款