basic_upload_apks_service_account.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2014 Marta Rodriguez.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the 'License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Uploads an apk to the chosen track."""
  17. import argparse
  18. from googleapiclient.discovery import build
  19. import httplib2
  20. from oauth2client.service_account import ServiceAccountCredentials
  21. from oauth2client import client
  22. import os
  23. import json
  24. # Declare command-line flags.
  25. argparser = argparse.ArgumentParser(add_help=False)
  26. argparser.add_argument(
  27. "package_name", help="The package name. Example: com.android.sample"
  28. )
  29. argparser.add_argument(
  30. "package_track",
  31. help="The track to deploy to." ' Can be "alpha", "beta", "production" or "rollout"',
  32. )
  33. argparser.add_argument("release_note", help="text to add as en-US release notes")
  34. argparser.add_argument("apk_files", nargs="*", help="Paths to the APK files to upload.")
  35. google_service_account_config = json.loads(os.environ["GOOGLE_SERVICE_ACCOUNT"])
  36. def main():
  37. # Create an httplib2.Http object to handle our HTTP requests and authorize it
  38. # with the Credentials. Note that the first parameter, service_account_name,
  39. # is the Email address created for the Service account. It must be the email
  40. # address associated with the key that was created.
  41. credentials = ServiceAccountCredentials.from_json_keyfile_dict(
  42. google_service_account_config,
  43. scopes="https://www.googleapis.com/auth/androidpublisher",
  44. )
  45. http = httplib2.Http()
  46. http = credentials.authorize(http)
  47. service = build("androidpublisher", "v3", http=http)
  48. # Process flags and read their values.
  49. flags = argparser.parse_args()
  50. package_name = flags.package_name
  51. package_track = flags.package_track
  52. release_note = flags.release_note
  53. apk_files = flags.apk_files
  54. try:
  55. edit_request = service.edits().insert(body={}, packageName=package_name)
  56. result = edit_request.execute()
  57. edit_id = result["id"]
  58. version_codes = list()
  59. for filepath in apk_files:
  60. apk_response = (
  61. service.edits()
  62. .apks()
  63. .upload(editId=edit_id, packageName=package_name, media_body=filepath)
  64. .execute()
  65. )
  66. print(
  67. "Version code {version_code} has been uploaded".format(
  68. version_code=apk_response["versionCode"]
  69. )
  70. )
  71. version_codes.append(apk_response["versionCode"])
  72. track_response = (
  73. service.edits()
  74. .tracks()
  75. .update(
  76. editId=edit_id,
  77. track=package_track,
  78. packageName=package_name,
  79. body={
  80. "track": package_track,
  81. "releases": [
  82. {
  83. "versionCodes": version_codes,
  84. "releaseNotes": [
  85. {"language": "en-US", "text": release_note}
  86. ],
  87. "status": "completed",
  88. }
  89. ],
  90. },
  91. )
  92. .execute()
  93. )
  94. print(
  95. "Track {track} is set for releases {releases}".format(
  96. track=track_response["track"], releases=track_response["releases"]
  97. )
  98. )
  99. commit_request = (
  100. service.edits().commit(editId=edit_id, packageName=package_name).execute()
  101. )
  102. print('Edit "{id}" has been committed'.format(id=commit_request["id"]))
  103. except client.AccessTokenRefreshError:
  104. print(
  105. "The credentials have been revoked or expired, please re-run the "
  106. "application to re-authorize"
  107. )
  108. if __name__ == "__main__":
  109. main()