以编程方式将 Android 应用程序上传到 Google Play 商店

Programmatically Uploading Android App to Google Play Store

我正在尝试构建一个助手 class 以将我的 Android 应用上传到 Google Play 商店测试轨道,作为我的 CI/CD 管道的一部分。我做了一堆谷歌搜索并找到了各种示例,但是 none 对我有用,而且它们(显然)都使用旧版本的 AndroidPublisher API 并调用已弃用的方法.

到目前为止,这是我拼凑的。我正在成功获取 GoogleCredentials 对象,但我不知道应该在请求代码中的什么位置插入 accessTokencredentials

public void uploadToGooglePlay(String applicationName, String packageName, String credentialFilePath, String aabFilePath) throws Throwable {
    NetHttpTransport httpTransport = new NetHttpTransport();

    JsonFactory jsonFactory = new GsonFactory();

    GoogleCredentials credentials = GoogleCredentials.fromStream(
            new FileInputStream(credentialFilePath)
    );

    credentials.createScoped(
            AndroidPublisherScopes.ANDROIDPUBLISHER
    );

    AccessToken accessToken = credentials.getAccessToken();

    HttpRequestInitializer httpRequestInitializer = httpTransport.createRequestFactory().getInitializer();

    AndroidPublisher.Builder androidPublisherBuilder = new AndroidPublisher.Builder(
            httpTransport,
            jsonFactory,
            httpRequestInitializer
    );

    androidPublisherBuilder.setApplicationName(applicationName);

    AndroidPublisher androidPublisher = androidPublisherBuilder.build();

    AndroidPublisher.Edits.Insert insert = androidPublisher.edits().insert(
            packageName,
            null
    );

    AppEdit appEdit = insert.execute(); // THIS IS LINE 73 IN THE ERROR BELOW

    String editId = appEdit.getId();

    FileContent fileContent = new FileContent(
            "application/octet-stream",
            new File(aabFilePath)
    );

    AndroidPublisher.Edits.Bundles.Upload upload = androidPublisher.edits().bundles().upload(
            packageName,
            editId,
            fileContent
    );

    Bundle bundle = upload.execute();

    System.out.println("Version Code: " + bundle.getVersionCode());
    System.out.println("SHA Hash: " + bundle.getSha256());
}

当我运行这段代码时,我得到以下错误:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
POST https://androidpublisher.googleapis.com/androidpublisher/v3/applications/com.whatever.app/edits
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:118)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:37)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.interceptResponse(AbstractGoogleClientRequest.java:428)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1111)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:514)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:455)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:565)
    at UploadToGooglePlay.uploadToGooglePlay(UploadToGooglePlay.java:73)
    at UploadToGooglePlay.main(UploadToGooglePlay.java:27)

我确定我遗漏了一些简单而明显的东西,但我已经为这个问题苦恼了一段时间。

在此先感谢您的帮助!

我明白了。工作代码片段是:

InputStream googlePlayServiceCredentials = new FileInputStream(
    credentialFilePath
);

GoogleCredentials googleCredentials = GoogleCredentials
    .fromStream(googlePlayServiceCredentials)
    .createScoped(AndroidPublisherScopes.ANDROIDPUBLISHER);

HttpRequestInitializer httpRequestInitializer = new HttpCredentialsAdapter(googleCredentials);

AndroidPublisher.Builder androidPublisherBuilder = new AndroidPublisher.Builder(
    httpTransport,
    jsonFactory,
    httpRequestInitializer
);

但是...

新的 Google OAuth 2 库不支持在 HttpRequestInitializer 上设置任意 connect/read 超时,因此在上传 AAB 文件时失败(这需要永远)。所以我现在将上面的代码用于不涉及上传 AAB 的操作,并且我使用下面的代码(其中包括已弃用的方法,但据我所知没有办法解决这个问题):

InputStream googlePlayServiceCredentials = new FileInputStream(
    credentialFilePath
);

GoogleCredential credential = GoogleCredential.fromStream(
    googlePlayServiceCredentials
).createScoped(
    Set.of(
        AndroidPublisherScopes.ANDROIDPUBLISHER
    )
);

HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
    @Override
    public void initialize(HttpRequest httpRequest) throws IOException {
        credential.initialize(httpRequest);
        httpRequest.setConnectTimeout(5 * 60 * 1000);
        httpRequest.setReadTimeout(5 * 60 * 1000);
    }
};

AndroidPublisher.Builder androidPublisherBuilder = new AndroidPublisher.Builder(
    httpTransport,
    jsonFactory,
    httpRequestInitializer
);