我正在尝试使用 App Store Connect API 中的销售报告 API 和 Python 脚本。
import jwt
import requests
import time
import json
KEY_ID = "XXXXXXXXX"
ISSUER_ID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
# EXPIRATION_TIME = int(round(time.time() + (20.0 * 60.0))) # 20 minutes timestamp
PATH_TO_KEY = '/Users/164187.victor/Documents/Credentials/App_Store_Connect_RR/AuthKey_XXXXXXXXX.p8'
# pseudo, removed secret info
# read the file, currently binary but have tried string too
with open(PATH_TO_KEY, 'r+b') as keyfile:
secret = keyfile.read()
expir = round(time.time() + 20 * 60)
# sign the token with the iss, time, key, and kid with the correct alg
token = jwt.encode({'iss': ISSUER_ID,
'exp': expir,
'aud': 'appstoreconnect-v1'},
secret, algorithm='ES256',
headers={'alg': 'ES256', 'kid': KEY_ID, 'typ': 'JWT'})
# decode the bytes and create the get request header
headers = {'Authorization': f'Bearer {token}'}
params = {'filter[reportSubType]': 'SUMMARY', 'filter[reportType]': 'SALES', 'filter[frequency]':'DAILY', 'filter[vendorNumber]': 'XXXXXXXX'}
# send the get request
r = requests.get('https://api.appstoreconnect.apple.com/v1/salesReports',
headers=headers, params=params)
# Write the response in a pretty printed JSON file
with open('output.json', 'w') as out:
out.write(json.dumps(r.json(), indent=4))
我在 json 输出文件中得到了这个结果:
{
"errors": [
{
"status": "401",
"code": "NOT_AUTHORIZED",
"title": "Authentication credentials are missing or invalid.",
"detail": "Provide a properly configured and signed bearer token, and make sure that it has not expired. Learn more about Generating Tokens for API Requests https://developer.apple.com/go/?id=api-generating-tokens"
}
]
}
我尝试过的:
有什么办法可以解决这个问题吗?我已经坚持了 1 周了,请帮忙
鉴于错误中的链接,https://developer.apple.com/go/?id=api-generate-tokens,您缺少 iat(Issued at Time) 和有效负载中的范围参数。
您的代码似乎源自this Gist,它使用authlib来创建JWT令牌。但是代码中的
import jwt
语句表明您已切换到PyJWT,不要混淆两个包,它们有不同的使用方式。
对于
PyJWT
:
import jwt
token_data = jwt.encode(
{
'iss': ISSUER_ID,
'aud': 'appstoreconnect-v1',
'exp': expiry
},
secret,
headers={
'kid': KEY_ID
},
algorithm='ES256'
)
print(token_data.decode('UTF-8'))
对于
authlib
:
from authlib.jose import jwt
token_data = jwt.encode(
{
"alg": "ES256",
"kid": KEY_ID,
"typ": "JWT"
},
{
"iss": ISSUER_ID,
"exp": expir,
"aud": "appstoreconnect-v1"
},
secret
)
print(token.decode())
对于这两种方式,请不要忘记在请求标头中使用之前调用
decode()
。
另一个更好的选择是使用 applaud – 一个用于访问 App Store Connect API 的 Python 客户端库,您只需传递凭据来初始化
Connection
对象,applaud
会为您完成剩下的工作:
# Create the Connection
connection = Connection(ISSUER_ID, KEY_ID, PRIVATE_KEY)
r = connection.sales_reports().filter(
report_sub_type=SalesReportsEndpoint.ReportSubType.SUMMARY, # or "SUMMARY"
report_type=SalesReportsEndpoint.ReportType.SALES, # or "SALES"
frequency=SalesReportsEndpoint.Frequency.MONTHLY, # or "MONTHLY"
report_date='2021-12',
vendor_number='12345678',
).get()
r.save('output.txt', decompress=True)
您可以在付款和财务报告中找到您的供应商编号。示例代码在这里。
完全公开,我是applaud的原作者。
使用 https://jwt.io/ 生成不记名令牌。使用这个: 算法:ES256 标题: { “alg”:“ES256”, "kid": "[你的密钥 ID]", “类型”:“智威汤逊” } 有效负载: { "iss": "[您的发行人 ID]", “iat”:1734613799, “经验”:1734614999, “aud”:“appstoreconnect-v1” } 请注意,“exp”距“iat”应小于 1200 秒。 将您的私钥作为下载的 p8 文件的完整文本插入到“验证签名”字段中。 从“编码”字段复制生成的不记名令牌。
发布https://api.appstoreconnect.apple.com/v1/authorization 使用您的不记名令牌。它对我有用。