Fixing the 403 Error in App Store Connect API Downloads

Hi to everyone, I've been attempting to automate the download of sales reports from App Store Connect through their API, but I've facing error 403:

"id" : "2e8d7856-21f7-4436-9aa3-f43c36913d21",
"status" : "403",
"code" : "FORBIDDEN_ERROR",
"title" : "This request is forbidden for security reasons",
"detail" : "The API key in use does not allow this request"

Below is the Python script I'm using to execute the API call, and I've confirmed my user role is set to admin:

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

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

KEY_ID = "***"
ISSUER_ID = "***"

headers = {
    "alg": "ES256",
    "kid": KEY_ID, 
    "typ": "JWT",
}

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

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

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

url = 'https://api.appstoreconnect.apple.com/v1/salesReports'

params = {
    'filter[frequency]': 'YEARLY',
    'filter[reportDate]': '2021',
    'filter[reportSubType]': 'DETAILED',
    'filter[reportType]': 'INSTALLS',
    'filter[vendorNumber]': '***'
}

headers = {
    'Authorization': f'Bearer {gen_jwt}'
}

response = requests.get(url, headers=headers, params=params)

if response.status_code == 200:
    with open('sales_report.csv', 'wb') as f:
        f.write(response.content)
else:
    print(f'Failed to download sales report: {response.status_code} - {response.text}')

I found in the forum some users (similar post) that had my same issue but it seem that there's no way out. So now I'm wondering if I made some mistake in my code.

I hope you can help me with this.

Thanks in advance.

Your issue seems to be the permissions of you Team Key which is not enough. A different issue happened to me without any error message and it was because of missing "Accept" header. Using "Accept: /" fixed the issue.

Fixing the 403 Error in App Store Connect API Downloads
 
 
Q