How to upload app bundle (.aab) to play store using Google Play Publisher API
Asked Answered
V

2

7

I have configured and used API to upload .apk files and it is working perfectly using this code file.

    """Uploads an apk to the alpha track."""

import argparse
import sys
from apiclient import sample_tools
from oauth2client import client

TRACK = 'alpha'  # Can be 'alpha', beta', 'production' or 'rollout'

# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('package_name',
                       help='The package name. Example: com.android.sample')
argparser.add_argument('apk_file',
                       nargs='?',
                       default='test.apk',
                       help='The path to the APK file to upload.')


def main(argv):
  # Authenticate and construct service.
  service, flags = sample_tools.init(
      argv,
      'androidpublisher',
      'v3',
      __doc__,
      __file__, parents=[argparser],
      scope='https://www.googleapis.com/auth/androidpublisher')

  # Process flags and read their values.
  package_name = flags.package_name
  apk_file = flags.apk_file

  try:
    edit_request = service.edits().insert(body={}, packageName=package_name)
    result = edit_request.execute()
    edit_id = result['id']

    apk_response = service.edits().apks().upload(
        editId=edit_id,
        packageName=package_name,
        media_body=apk_file).execute()

    print 'Version code %d has been uploaded' % apk_response['versionCode']

    track_response = service.edits().tracks().update(
        editId=edit_id,
        track=TRACK,
        packageName=package_name,
        body={u'releases': [{
            u'name': u'My first API release',
            u'versionCodes': [str(apk_response['versionCode'])],
            u'status': u'completed',
        }]}).execute()

    print 'Track %s is set with releases: %s' % (
        track_response['track'], str(track_response['releases']))

    commit_request = service.edits().commit(
        editId=edit_id, packageName=package_name).execute()

    print 'Edit "%s" has been committed' % (commit_request['id'])

  except client.AccessTokenRefreshError:
    print ('The credentials have been revoked or expired, please re-run the '
           'application to re-authorize')

if __name__ == '__main__':
  main(sys.argv)

But this code does not work for app bundles.

Verret answered 9/4, 2019 at 9:12 Comment(2)
You haven't given enough information for anyone to help you. How do you change the code for aab files (do you change the filename?). When you say it "does not work" what is the error message you get?Hashimoto
Note that the code is largely copied from here: github.com/googlesamples/android-play-publisher-api/blob/master/…Gigi
A
8

If googleapiclient.errors.UnknownFileType exception occurs, try adding the following code:

import mimetypes

mimetypes.add_type("application/octet-stream", ".apk")
mimetypes.add_type("application/octet-stream", ".aab")

And if socket.timeout exception occurs, try adding the following code:

import socket

socket.setdefaulttimeout(7 * 24 * 60 * 60)
Anguilliform answered 8/9, 2019 at 11:55 Comment(0)
F
7

Google publishing API exposes bundles method. You can try following:

service.edits().bundles().upload(
        editId=edit_id,
        packageName=package_name,
        media_body=aab_file).execute()
Flattie answered 17/6, 2019 at 19:16 Comment(1)
I had to add the media_mime_type="application/octet-stream" argument to this method anyway.Acquittance

© 2022 - 2024 — McMap. All rights reserved.