美文网首页
Python 调用 AppStore Connect Api 为

Python 调用 AppStore Connect Api 为

作者: 试图与自己和解 | 来源:发表于2024-04-09 15:35 被阅读0次

AppStore Connect Api 官方文档

环境配置

python3 环境为例

1.安装 jwt

pip3 install PyJWT

2.安装 requests

pip3 install requests

物料配置

一、生成 kid

要生成密钥,需在 App Store Connect 中具有管理员角色或帐户持有人角色。登录 App Store Connect 并完成以下步骤:

  1. 选择 “用户和访问”,然后选择 “密钥” 子标签页。
  2. 在 “密钥类型” 下选择 “App内购买项目”。
  3. 单击 “生成API内购买项目密钥”(如果之前创建过,则点击 “添加(+)” 按钮新增。)。
  4. 输入密钥的名称(随意)。
  5. 单击 “生成”。
image.png image.png
二、生成 Issuer ID (iss)
image.png image.png

同时下载密码文件,以作为生成 JWT 的秘钥使用。注意:秘钥仅能下载一次,请妥善保管、备份!

image.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 分钟。

拓展:
探索 JWT:安全、可扩展的身份验证方案
JWT官方地址 -- 不同语言生成JWT

相关文章

网友评论

      本文标题:Python 调用 AppStore Connect Api 为

      本文链接:https://www.haomeiwen.com/subject/qzkfxjtx.html