Getting Status 401 Unauthenticated while calling app store server notifications to send a test notification

I'm trying to create a test notification, but getting Unauthorized from the API. I've created an In-App Purchase key, and using the following code to generate the JWT Token:

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

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

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

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


with open("SubscriptionKey_B7J<..>.p8", "rb") as fh: 
    signing_key = fh.read()

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

print(f"{gen_jwt}")

One of the request IDs that failed: FPJYAOSUZTFXXIKUELW6QE7F54.0.0

Replies

Thank you for reaching out and providing the request id of a failed request. Unfortunately all we can see from our side is an invalid signature. Reading over your code, it seems to be functioning correctly. Can you confirm that the key has not been removed from your App Store Connect page? Also, confirm the iss field is correct.

Yes, the key is present in ASC. Also, all the fields are correct. Are there any other pre-requisites that I might be missing to use the API?

Please file a ticket in Feedback Assistant and mention the App Store Server API and include your unobfuscated code, request id, and an expired JWT that you've generated, and we'll take a look.