我们发布了一款iOS应用程序,我想从App Store Connect API中获取一些信息(例如安装、更新和评论)。根据苹果公司的官方文档创建一个JSON Web令牌:链接。然后在标头中使用令牌进行请求。现在每次都会得到“401”|“NOT_AUTHORIZED”的回答,如下图所示:REST Response。
以下是Python代码片段(我尝试在Python和R中解决它,但结果总是相同)。首先,创建JWT:
以下是Python代码片段(我尝试在Python和R中解决它,但结果总是相同)。首先,创建JWT:
from datetime import datetime, timedelta from jose import jwt, jws import ecdsa现在,当我打印编码时,我得到以下JWT(对我来说看起来有效):
KEY_ID = "XXXXXXXXXX" ISSUER_ID = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" PRIVATE_KEY = open('AuthKey_XXXXXXXXXX.p8', 'r').read() TIMESTAMP = int( (datetime.now() - timedelta(minutes = 45)).timestamp() * 1000)
claim = {"iss" : ISSUER_ID, "exp" : TIMESTAMP, "aud" : "appstoreconnect-v1"}
header = { "alg": "ES256", "kid": KEY_ID, "typ": "JWT" }
# Create the JWT encoded = jwt.encode(claim, PRIVATE_KEY, algorithm='ES256', headers=header)
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiIsImtpZCI6IlhYWFhYWFhYWFgifQ.eyJpc3MiOiJYWFhYWFhYWC1YWFhYLVhYWFgtWFhYWC1YWFhYWFhYWFhYWFgiLCJleHAiOjE1NDUzOTc1MTQ1ODAsImF1ZCI6ImFwcHN0b3JlY29ubmVjdC12MSJ9.eTl6iaAW-Gp67FNmITrWCpLTtJzVdLYXIl5_KKgqaNgzwyGo7npBOBo9_u5PtLNnssQFEwJWbPND-6Ww5ACgEg'即使我通过Base64解码JWT的前两部分,我也可以得到正确的标题(它还包含正确的编码算法:“alg”:“ES256”)和声明。
从jose.utils中导入base64url_decode print(base64url_decode(b'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiIsImtpZCI6IlhYWFhYWFhYWFgifQ')) print(base64url_decode(b'eyJpc3MiOiJYWFhYWFhYWC1YWFhYLVhYWFgtWFhYWC1YWFhYWFhYWFhYWFgiLCJleHAiOjE1NDUzOTc1MTQ1ODAsImF1ZCI6ImFwcHN0b3JlY29ubmVjdC12MSJ9'))
查看下图:Base64解码输出
现在,我认为JWT对象已经准备好了,我将请求发送到API:
import requests
JWT = 'Bearer ' + encoded
URL = 'https://api.appstoreconnect.apple.com/v1/apps' HEAD = {'Authorization': JWT} print(HEAD)
R = requests.get(URL, headers=HEAD) R.json()
现在我们可以看到我的问题,参见图片:Header | REST Response
请注意,我已经隐藏了示例中的KEY_ID、ISSUER_ID和PRIVATE_KEY。