bbd*_*108 4 python jwt python-requests
为了生成 API 请求的令牌,苹果概述了以下步骤。
、key、kid和iss均已被验证可以工作。然而在下面的 python 脚本中,
import jwt
import requests
# pseudo, removed secret info
# read the file, currently binary but have tried string too
with open('AuthKey_4..._.p8', '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': '6...',
'exp': f'{expir}',
'aud': 'appstoreconnect-v1'},
secret, algorithm='ES256',
headers={'alg': 'ES256', 'kid': '4...', 'typ': 'JWT'})
# decode the bytes and create the get request header
s_token = token.decode('utf-8')
headers = {'Authorization': f'Bearer {s_token}'}
# send the get request
r = requests.get('https://api.appstoreconnect.apple.com/v1/salesReports',
headers=headers)#, params=params)
Run Code Online (Sandbox Code Playgroud)
r.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'}]}
Run Code Online (Sandbox Code Playgroud)
此外,错误消息中的链接似乎也已损坏。
我尝试.p8以二进制和常规字符串表示形式读取文件。我尝试过在令牌中传递不同的值、删除某些值等。我还尝试过不将有效负载参数传递到 GET 请求中,这也会导致 401 错误。此处列出了有效负载信息。任何帮助表示赞赏。
exp 不能是字符串...
import jwt
import requests
import time
# pseudo, removed secret info
# read the file, currently binary but have tried string too
with open('AuthKey_4..._.p8', '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': '6...',
'exp': expir,
'aud': 'appstoreconnect-v1'},
secret, algorithm='ES256',
headers={'alg': 'ES256', 'kid': '4...', 'typ': 'JWT'})
# create the get request header
headers = {'Authorization': f'Bearer {token}'}
# send the get request
r = requests.get('https://api.appstoreconnect.apple.com/v1/salesReports',
headers=headers)#, params=params)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2238 次 |
| 最近记录: |