环境配置
以 python3
环境为例
1.安装 jwt
pip3 install PyJWT
2.安装 requests
pip3 install requests
物料配置
一、生成 kid
要生成密钥,需在 App Store Connect 中具有管理员角色或帐户持有人角色。登录 App Store Connect 并完成以下步骤:
- 选择 “用户和访问”,然后选择 “密钥” 子标签页。
- 在 “密钥类型” 下选择 “App内购买项目”。
- 单击 “生成API内购买项目密钥”(如果之前创建过,则点击 “添加(+)” 按钮新增。)。
- 输入密钥的名称(随意)。
- 单击 “生成”。
![](https://img.haomeiwen.com/i2030826/27d4786abceefe02.png)
![](https://img.haomeiwen.com/i2030826/c5ca86a62a0e096e.png)
二、生成 Issuer ID (iss)
![](https://img.haomeiwen.com/i2030826/68c268d0750f7e84.png)
![](https://img.haomeiwen.com/i2030826/dec1984e4a093452.png)
同时下载密码文件,以作为生成 JWT
的秘钥使用。注意:秘钥仅能下载一次,请妥善保管、备份!
![](https://img.haomeiwen.com/i2030826/577b42a6b7d07c82.png)
有了以上这些参数及秘钥,我们就可以生成 jwt
去调用 Apple Api 了。
Python 示例脚本
下面是一个批量为app发送内测邀请邮件的脚本,使用的是 Apple Api : api.appstoreconnect.apple.com/v1/userInvitations
# import json
import time
from datetime import datetime, timedelta
import jwt
import requests
def get_token():
# 这里替换成刚下载的秘钥
private_key = open('/xx/xx/xxx/xxxx.p8', 'r').read()
# 构造header
header = {
"alg": 'ES256',
#这里替换为生成的kid
"kid": '这里替换为生成的kid',
"typ": "JWT"
}
# 构造payload
payload = {
# 这里替换为生成的iss
"iss": 'xxxx-xxxxx-xxxxx',
"exp": int(time.mktime((datetime.now() + timedelta(minutes=20)).timetuple())),
"aud": "appstoreconnect-v1"
}
token = jwt.encode(payload=payload, key=private_key,
algorithm='ES256', headers=header)
print("调用API的token:", token)
return token
# 替换为你的访问凭据
access_token = get_token()
# 替换为你要添加内测人员的应用ID
app_id = "xxxxxx"
# 替换为你要添加的内测人员的列表
users = [
{"email": "xxxxx@xx.com","firstName": "a","lastName": "cs"}
]
# 批量邀请用户
def invite_users(email, firstName, lastName ):
url = "https://api.appstoreconnect.apple.com/v1/userInvitations"
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
data = {
"data": {
"type": "userInvitations",
"attributes": {
"email": email,
"firstName": firstName,
"lastName": lastName,
"roles": ["DEVELOPER"],
"allAppsVisible": True
}
}
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 201:
print("用户邀请成功!:", response.content)
else:
print("用户邀请失败:", response.text)
# 调用函数批量邀请用户
for user in users:
email = user["email"]
firstName = user["firstName"]
lastName = user["lastName"]
invite_users(email, firstName , lastName)
注意:每一次 api 调用,都得在 headers
里面塞入生成的 token,且 token 目前 Apple 限制能设置的最长有效周期为 20 分钟。
网友评论