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.

hard coder
  • 5,449
  • 6
  • 36
  • 61
  • 1
    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? – Nick Fortescue Apr 10 '19 at 08:41
  • Note that the code is largely copied from here: https://github.com/googlesamples/android-play-publisher-api/blob/master/v3/python/basic_upload_apks.py – Richard Möhn Jul 18 '22 at 06:55

2 Answers2

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)
Joonsoo
  • 788
  • 1
  • 13
  • 15
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()
Rakesh
  • 4,004
  • 2
  • 19
  • 31
  • I had to add the `media_mime_type="application/octet-stream"` argument to this method anyway. – Kaspi Jan 31 '22 at 15:56