使用Play API上传App Bundle失败问题求助
解决草稿应用通过Android Publisher API上传Bundle并配置内部测试的问题
我有一个自动上传App Bundle并在Play商店创建内部测试版本的脚本,在非草稿应用上运行正常。但针对一款处于草稿状态的新应用(暂无生产版本),想要上传Bundle给内部测试人员更新时,脚本执行报错。手动上传Bundle并创建内部测试版本是可行的,但脚本无法运行。
错误信息
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://androidpublisher.googleapis.com/androidpublisher/v3/applications/com.company.app.app/edits/18014957208426515290:commit?alt=json returned "Only releases with status draft may be created on draft app.". Details: "Only releases with status draft may be created on draft app.">
原脚本
import os from googleapiclient.discovery import build from oauth2client.service_account import ServiceAccountCredentials # Get the app ID from the environment. app_id = "com.company.app.app" print(f"App ID set to {app_id}.") # Get the APK file path from the environment. aab_file_path = "/output/com.company.app.app-Signed.aab" print(f"Path to bundle set to {aab_file_path}.") # Get the service account JSON file path from the environment. service_account_file_path = "/service-account-key.json" print(f"Path to service account key set to {service_account_file_path}.") # Create the service account credentials object. credentials = ServiceAccountCredentials.from_json_keyfile_name( service_account_file_path, ["https://www.googleapis.com/auth/androidpublisher"] ) # Create the API client object. service = build("androidpublisher", "v3", credentials=credentials) # Get an edit ID. print(f"Creating an edit for bundle upload...") request = service.edits().insert(packageName=app_id) response = request.execute() editId = response['id'] print(f"Edit ID for bundle upload is {editId}.") with open(aab_file_path, "rb") as f: request = service.edits().bundles().upload( packageName=app_id, editId=editId, media_body=aab_file_path, media_mime_type="application/octet-stream", ) print(f"Creating edit for bundle upload...") response = request.execute() versionCode = response.get("versionCode") print(f"Bundle version code is {versionCode}.") print(f"Commiting bundle upload edit...") request = service.edits().commit(editId=editId, packageName=app_id) response = request.execute() print(f"Bundle upload successfull.") request = service.edits().insert(packageName=app_id) response = request.execute() editId = response['id'] release = { "versionCodes": [ versionCode ], "releaseNotes": [ { "language": "en_GB", "text": "Minor Improvements" } ], "status": "draft", } track = { "track": "internal", "releases": [ release ] } request = service.edits().tracks().update(packageName=app_id, editId=editId, track="internal", body=track) response = request.execute() request = service.edits().commit(packageName=app_id, editId=editId) response = request.execute()
解决方案
问题原因
对于处于草稿状态的应用,Play商店API不允许单独上传Bundle并提交编辑(此时系统会默认尝试创建非草稿版本,违反草稿应用的限制)。必须在**同一个编辑会话(edit)**中完成Bundle上传和内部测试草稿版本的配置,再一起提交。
修改后的脚本
import os from googleapiclient.discovery import build from oauth2client.service_account import ServiceAccountCredentials # Get the app ID from the environment. app_id = "com.company.app.app" print(f"App ID set to {app_id}.") # Get the APK file path from the environment. aab_file_path = "/output/com.company.app.app-Signed.aab" print(f"Path to bundle set to {aab_file_path}.") # Get the service account JSON file path from the environment. service_account_file_path = "/service-account-key.json" print(f"Path to service account key set to {service_account_file_path}.") # Create the service account credentials object. credentials = ServiceAccountCredentials.from_json_keyfile_name( service_account_file_path, ["https://www.googleapis.com/auth/androidpublisher"] ) # Create the API client object. service = build("androidpublisher", "v3", credentials=credentials) # 创建单个编辑会话,用于上传Bundle和配置测试版本 print(f"Creating an edit for bundle upload and test release...") request = service.edits().insert(packageName=app_id) response = request.execute() editId = response['id'] print(f"Edit ID is {editId}.") # 上传App Bundle with open(aab_file_path, "rb") as f: request = service.edits().bundles().upload( packageName=app_id, editId=editId, media_body=aab_file_path, media_mime_type="application/octet-stream", ) response = request.execute() versionCode = response.get("versionCode") print(f"Bundle version code is {versionCode}.") # 配置内部测试的草稿版本 release = { "versionCodes": [versionCode], "releaseNotes": [ { "language": "en_GB", "text": "Minor Improvements" } ], "status": "draft", } track = { "track": "internal", "releases": [release] } request = service.edits().tracks().update( packageName=app_id, editId=editId, track="internal", body=track ) response = request.execute() print(f"Configured internal draft release successfully.") # 提交整个编辑会话 print(f"Committing edit...") request = service.edits().commit(editId=editId, packageName=app_id) response = request.execute() print(f"Bundle upload and test release setup succeeded.")
修改说明
- 将原本两个独立的
edit会话合并为一个,确保Bundle上传和测试版本配置在同一操作中完成。 - 删除了原脚本中单独提交Bundle上传的步骤,改为在配置完测试版本后统一提交编辑,符合草稿应用的API限制。
内容的提问来源于stack exchange,提问作者Julien Debache
相关产品推荐
相关产品推荐

