-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_play_products.py
executable file
·86 lines (65 loc) · 2.46 KB
/
google_play_products.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
#!/usr/bin/python3
import sys
import getopt
import json
from google.auth.exceptions import DefaultCredentialsError
from google.oauth2 import service_account
from googleapiclient import discovery, errors
from pyasn1.error import SubstrateUnderrunError
STATUS_CODE_EMOJI = {}
STATUS_CODE_EMOJI[404] = "❓"
STATUS_CODE_EMOJI[401] = "🔐"
STATUS_CODE_EMOJI[400] = "💣"
STATUS_CODE_EMOJI[000] = "🚨"
def fetch_iap_products(service_credentials_path, package_name):
try:
credentials_response = service_account.Credentials.from_service_account_file(
service_credentials_path)
except ValueError as error:
print(f"{STATUS_CODE_EMOJI[401]} {error}")
return
except DefaultCredentialsError as error:
print(f"{STATUS_CODE_EMOJI[401]} {error}")
return
except SubstrateUnderrunError as error:
print(f"{STATUS_CODE_EMOJI[401]} The service credentials are malformed")
return
if type(credentials_response) is service_account.Credentials:
credentials = credentials_response
else:
print(f"{STATUS_CODE_EMOJI[401]} Unknown error with service account credentials.")
return
service = discovery.build(
"androidpublisher", "v3", credentials=credentials, cache_discovery=False
)
request = (
service.inappproducts()
.list(packageName=package_name)
)
try:
response = request.execute()
except errors.HttpError as error:
error_content = json.loads(error.content)
if "error" in error_content:
error = error_content["error"]
if "code" in error:
status_code = error["code"]
else:
status_code = 000
if "message" in error:
message = error["message"]
else:
message = "Unknown error"
# If this message is Invalid Value, it may mean a subscription token sent to the onetime purchase API
# or a onetime purchase token sent to the subscription API
print(f"{STATUS_CODE_EMOJI[status_code]} {message}")
else:
print(response)
if __name__ == "__main__":
help_message = "google_token_validator.py /path/to/service_credentials.json [package_name] <OPTIONAL: --quiet>"
if len(sys.argv) < 3:
print(help_message)
sys.exit(2)
service_credentials = sys.argv[1]
package_name = sys.argv[2]
fetch_iap_products(service_credentials, package_name)