-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.py
152 lines (136 loc) · 4.88 KB
/
upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import argparse
import json
import os
import sys
from datetime import datetime, timedelta
import jwt
import requests
ACCESS_TOKEN_LIFESPAN = timedelta(minutes=10)
EDIT_ID_LIFESPAN = timedelta(minutes=10)
REQUESTS_TIMEOUT = 300
def check_response(response: requests.Response):
if not response.ok:
print(
(f'FAILED: \'{response.request.method} {response.request.url}\' failed '
f'with HTTP {response.status_code}:\n{response.text}'),
file=sys.stderr)
exit(1)
def obtain_access_token(key_file: dict) -> str:
print('obtaining access token...')
exp = datetime.now() + ACCESS_TOKEN_LIFESPAN
claim_set = {
'iss': key_file['client_email'],
'scope': 'https://www.googleapis.com/auth/androidpublisher',
'aud': 'https://oauth2.googleapis.com/token',
'exp': exp.strftime('%s'),
'iat': datetime.now().strftime('%s')
}
private_key = key_file['private_key'].encode('utf-8')
assertion = jwt.encode(
payload=claim_set,
key=private_key,
algorithm='RS256',
headers={'alg': 'RS256', 'typ': 'JWT'})
data = {
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion': assertion
}
response = requests.post(
url='https://oauth2.googleapis.com/token',
data=data,
timeout=REQUESTS_TIMEOUT)
check_response(response)
access_token = response.json()['access_token']
print(f'obtained access_token: ****({len(access_token)})')
return access_token
def obtain_edit_id(access_token: str, package_name: str) -> str:
print('obtaining edit_id...')
expiry_time = (datetime.now() + EDIT_ID_LIFESPAN).strftime('%s')
app_edit = {
'expiryTimeSeconds': expiry_time
}
response = requests.post(
url=f'https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{package_name}/edits',
json=app_edit,
headers={'Authorization': f'Bearer {access_token}'},
timeout=REQUESTS_TIMEOUT)
check_response(response)
edit_id = response.json()['id']
print(f'obtained {edit_id=}')
return edit_id
def upload_aab(access_token: str, aab_path: str, package_name: str, edit_id: str):
print('uploading aab...')
with open(aab_path, 'rb') as f:
data = f.read()
response = requests.post(
url=f'https://androidpublisher.googleapis.com/upload/androidpublisher/v3/applications/{package_name}/edits/{edit_id}/bundles',
data=data,
headers={
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/octet-stream'
},
timeout=REQUESTS_TIMEOUT
)
check_response(response)
uploaded_bundle = response.json()
version_code = uploaded_bundle['versionCode']
sha256 = uploaded_bundle['sha256']
print(f'successfully uploaded aab: {version_code=}, {sha256=}')
def commit_edit(access_token: str, package_name: str, edit_id: str):
print(f'commiting {edit_id=}...')
response = requests.post(
url=f'https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{package_name}/edits/{edit_id}:commit',
headers={'Authorization': f'Bearer {access_token}'},
timeout=REQUESTS_TIMEOUT)
check_response(response)
print(f'committed {edit_id=}')
def main():
parser = argparse.ArgumentParser(description="Google Play AAB uploader")
parser.add_argument(
'--key-path',
type=str,
required=False,
default=None,
dest='key_path',
help='Path to service account key file')
parser.add_argument(
'--key-json',
type=str,
required=False,
default=None,
dest='key_json',
help='Service account key JSON')
parser.add_argument(
'--package-name',
type=str,
required=True,
dest='package_name',
help='Package name of an app')
parser.add_argument(
'--aab-path',
type=str,
required=True,
dest='aab_path',
help='Path to app bundle file')
args = parser.parse_args()
if args.key_path:
if not os.path.exists(args.key_path):
print(f'key file does not exist: {args.key_path}', file=sys.stderr)
exit(1)
with open(args.key_path) as f:
key_file = json.load(f)
elif args.key_json:
key_file = json.loads(args.key_json)
else:
print(f'--key-file or --key-json is required', file=sys.stderr)
exit(1)
if not os.path.exists(args.aab_path):
print(f'aab file does not exist: {args.aab_path}', file=sys.stderr)
exit(1)
access_token = obtain_access_token(key_file)
edit_id = obtain_edit_id(access_token, args.package_name)
upload_aab(access_token, args.aab_path, args.package_name, edit_id)
commit_edit(access_token, args.package_name, edit_id)
print('done')
if __name__ == "__main__":
main()