[Python] Generating JWT token to make requests to Apple

Discussão em 'Python' iniciado por Stack, Março 11, 2025.

  1. Stack

    Stack Membro Participativo

    I am using the following code to make a request to Apple. Then I make the following curl request but I'm getting a 401. How can I resolve this?

    from datetime import datetime, timedelta
    from time import time, mktime
    import jwt

    dt = datetime.now() + timedelta(minutes=19)

    headers = {
    "alg": "ES256",
    "kid": "<KEY>",
    "typ": "JWT",
    }


    payload = {
    "iss": "<ID>",
    "iat": int(time()),
    "exp": int(mktime(dt.timetuple())),
    "aud": "appstoreconnect-v1",
    }


    with open("AuthKey.p8", "rb") as fh: # Add your file
    signing_key = fh.read()

    gen_jwt = jwt.encode(payload, signing_key, algorithm="ES256", headers=headers)

    print(f"[JWT] {gen_jwt}")


    Console:

    curl -v -X PUT "https://api.storekit.itunes.apple.com/inApps/v1/transactions/consumption/<id>/" \
    -H "Authorization: Bearer <JWT>" \
    -H "Content-Type: application/json" \
    -d '{
    "accountTenure": 2,
    "appAccountToken": "",
    "consumptionStatus": 3,
    "customerConsented": true,
    "deliveryStatus": 0,
    "lifetimeDollarsPurchased": 1,
    "lifetimeDollarsRefunded": 1,
    "platform": 1,
    "playTime": 6,
    "refundPreference": 2,
    "sampleContentProvided": true,
    "userStatus": 1
    }'


    I validated the jwt token and it’s correct. I got the private key by going to user access - App Store Connect - team keys

    Continue reading...

Compartilhe esta Página