2

Apple サインインとデバイス チェックのために Python で認証コード/クライアント シークレットを生成するにはどうすればよいですか?

4

1 に答える 1

3
  1. まず、アプリ固有の p8 ファイル (pem 形式の秘密鍵) を生成する必要があります。これには次の手順を実行します。
  • Apple 開発者ポータルに移動し、証明書の識別子とプロファイルの Apple => キーの下に移動します。
  • + 記号をクリックして、使用するサービスでキーを作成します
  • 次に、p8 ファイルをダウンロードします (紛失しないように注意してください。再度ダウンロードすることはできません)。
  • 後で必要になるキーIDもコピーします
  1. Python で pyjwt をインストールし、次の手順を実行します。
  • ペイロード dict を作成します。
         
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()
于 2020-12-30T19:55:56.947 に答える