我已经配置并使用了API来上传.apk文件,并且使用此代码文件可以正常运行。
"""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)
但是此代码不适用于应用程序捆绑包。
答案 0 :(得分:0)
Google发布API公开了bundles方法。您可以尝试以下操作:
service.edits().bundles().upload(
editId=edit_id,
packageName=package_name,
media_body=aab_file).execute()
答案 1 :(得分:0)
如果发生googleapiclient.errors.UnknownFileType
异常,请尝试添加以下代码:
import mimetypes
mimetypes.add_type("application/octet-stream", ".apk")
mimetypes.add_type("application/octet-stream", ".aab")
如果发生socket.timeout
异常,请尝试添加以下代码:
import socket
socket.setdefaulttimeout(7 * 24 * 60 * 60)