SpringBoot]App StoreConnect APIを使用したアプリケーション情報のインポート


App Store API


AppleはAppstore Open APIを提供しています.APIの1つはApp Store Connect APIです.
https://developer.apple.com/documentation/appstoreconnectapi
このAPIは、アプリケーションの自動化、アプリケーション・メタデータ、およびアプリケーションのパブリケーションなど、さまざまな機能を提供します.しかし、これらのAPIを使用するには、API鍵、プライベート鍵、およびJWTが必要である.
API Key、Private Keyリリースについては、次のアドレスを参照してください.
https://developer.apple.com/documentation/appstoreconnectapi/creating_api_keys_for_app_store_connect_api
API Key、Private Keyが準備できたら、JWTを送信してライセンスを取得し、App Store Connect APIで複数の機能を有効にすることができます.
JWTを送信するには、対応するライブラリを構築する必要があります.(他のライブラリでも使用可能です.)
implementation 'com.nimbusds:nimbus-jose-jwt:3.10'
Spring bootは自分の好きなバージョンを使うことができ、必ずJava 8バージョンを使う.
Private Keyの設定中にjava 8のみで実行されるコードが存在するため!この文章は正確ではありません.参考までに...!

インプリメンテーション



App Store Connect API用のJWTヘッダ条件.algはES 256、typeはJWTに設定されており、最後にkidは対応するサイトを参照してフォローしてください.JWT Headerを設定するコードを次に示します.
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.ES256).keyID(keyId).type(JOSEObjectType.JWT).build();
ヘッドの設定後、Payloadに送信する情報を設定する必要があります.次の情報はPayloadに含める必要があります.API呼び出し情報をscopeに入れればよい.

呼び出しURLがGET https://api.appstoreconnect.apple.com/v1/apps/{id}/appInfosであればscopeに「GET/v 1/apps/{appId}/appInfos」と入力すればよい.実装コードは次のとおりです.
  
  JWTClaimsSet claimsSet = new JWTClaimsSet();
  Date now = new Date();
  claimsSet.setIssuer(issuer_Id);
  claimsSet.setIssueTime(now);
  claimsSet.setExpirationTime(new Date(now.getTime()+3600000));
  claimsSet.setAudience("appstoreconnect-v1");
  claimsSet.setClaim("scope", "GET /v1/apps/"+appId+"/appInfos");
HeaderとPayloadが設定されているので、署名を記入するだけでJWTが完了します.
SignedJWT jwt = new SignedJWT(header,claimsSet);

try{
	ECPrivateKey ecPrivateKey = new ECPrivateKeyImpl(readPrivateKey(keyPath));
  JWSSigner jwsSigner = new ECDSASigner(ecPrivateKey.getS());
  jwt.sign(jwsSigner);

 }catch(InvalidKeyException e)
  {
     e.printStackTrace();
  }catch (JOSEException e)
  {
     e.printStackTrace();
  }
完全なコードは次のとおりです.
 public String createJWT( )
 {
      JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.ES256).keyID(keyId).type(JOSEObjectType.JWT).build();

      JWTClaimsSet claimsSet = new JWTClaimsSet();
      Date now = new Date();
      claimsSet.setIssuer(issuer_Id);
      claimsSet.setIssueTime(now);
      claimsSet.setExpirationTime(new Date(now.getTime()+3600000));
      claimsSet.setAudience("appstoreconnect-v1");
      claimsSet.setClaim("scope", "GET /v1/apps/"+appId+"/appInfos");

      SignedJWT jwt = new SignedJWT(header,claimsSet);

      try{
          ECPrivateKey ecPrivateKey = new ECPrivateKeyImpl(readPrivateKey(keyPath));
          JWSSigner jwsSigner = new ECDSASigner(ecPrivateKey.getS());
          jwt.sign(jwsSigner);

        }catch(InvalidKeyException e)
        {
            e.printStackTrace();
        }catch (JOSEException e)
        {
            e.printStackTrace();
        }

        return jwt.serialize();

  }
JWTが完了し、JWTの転送が終了しました.JWTをバインドするコードを次に示します.
 public String getAppInfos(String jwt) throws MalformedURLException {
        System.out.println("jwt = " + jwt);
        String result = "";
        URL url = new URL("https://api.appstoreconnect.apple.com/v1/apps");

        try{
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();

            urlConnection.setRequestMethod("GET");
            urlConnection.setRequestProperty("Authorization", "Bearer "+jwt);

            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

            int responseCode = urlConnection.getResponseCode();
            System.out.println("responseCode = " + responseCode);

            String line = "";
            String res = "";
            while((line=br.readLine())!=null)
            {
                res+=line;
            }

            System.out.println("res = " + res);
            result = res;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
足りない文章ですが、読んでくれてありがとう.