gpt4 book ai didi

Apple Store Connect 的 Python JSON Web token (JWT)GET 请求 401 错误

转载 作者:行者123 更新时间:2023-12-05 01:38:26 24 4
gpt4 key购买 nike

为了生成 API 请求的 token ,苹果概述了 following steps .

keykidiss 都已验证可以正常工作。但是在下面的 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)

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'}]}

此外,错误消息中的链接似乎也已损坏。

我尝试以二进制和常规字符串表示形式读取 .p8 文件。我试过在 token 中传递不同的值,删除某些值等。我也尝试过不将有效负载参数传递到 GET 请求中,这也会导致 401 错误。有效负载信息已列出 here .任何帮助表示赞赏。

最佳答案

exp 不能是字符串...

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': 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)

关于Apple Store Connect 的 Python JSON Web token (JWT)GET 请求 401 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59886504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com