【问题标题】:How to generate apple authorization token/client secret?如何生成苹果授权令牌/客户端密码?
【发布时间】:2021-04-07 07:18:32
【问题描述】:

如何在 python 中为苹果登录和设备检查生成授权码/客户端密码?

【问题讨论】:

    标签: python jwt apple-sign-in devicecheck


    【解决方案1】:
    1. 首先,我们需要生成一个特定于应用的 p8 文件(pem 格式的私钥),为此执行以下操作:
    • 转到您的苹果开发者门户,在证书下 标识符和配置文件苹果 => 键
    • 点击 + 号并创建一个包含您要使用的服务的密钥
    • 然后下载p8文件(注意不要丢失,不能再次下载)
    • 同时复制您以后需要的密钥 ID
    1. 在 python 中安装 pyjwt 并执行以下操作:
    • 创建有效载荷字典:
             
    data = {
        "iss": "team_id", # team id of your developer account this can be found in your apple developer portal => identifier of your app => "App ID prefix"
        "iat": timestamp_now, # creation timestamp in seconds
        "exp": timestamp_exp, # expiration timestamp in seconds (max 20 mins) see 
        "aud": "https://appleid.apple.com",
        "sub": client_id # your bundle
    }
    
    
    • 打开私钥(您在步骤 1 中下载的)并将其读入变量
    with open("filename.p8", "r") as f:
        private_key = f.read()
    
    • 生成您的签名 jwt 令牌:
    token = jwt.encode(payload=data, key=private_key, algorithm="ES256", headers={
        "kid":key_id # the key id is the id u saved in step 1
    }).decode()
    
    • jwt.encode 返回字节,如果你想要它作为一个字符串你需要像我一样解码它

    完整的代码如下所示

    import jwt
    
    def generate_token()
            with open("filename.p8", "r") as f:
                private_key = f.read()
            team_id = "teamid"
            client_id = "bundle.id"
            key_id = "keyid"
            validity_minutes = 20
            timestamp_now = int(utils.time_stamp_seconds())
            timestamp_exp = timestamp_now + (60 * validity_minutes)
            cls.last_token_expiration = timestamp_exp
            data = {
                    "iss": team_id,
                    "iat": timestamp_now,
                    "exp": timestamp_exp,
                    "aud": "https://appleid.apple.com",
                    "sub": client_id
                }
            token = jwt.encode(payload=data, key=private_key, algorithm="ES256", headers={"kid": key_id}).decode()
    

    【讨论】:

      猜你喜欢
      • 2021-01-02
      • 2020-01-07
      • 2019-12-25
      • 2019-11-15
      • 1970-01-01
      • 2020-12-31
      • 2017-01-26
      • 2019-08-06
      • 1970-01-01
      相关资源
      最近更新 更多