|
| 1 | +# This is a sample Python script. |
| 2 | +import json |
| 3 | +import random |
| 4 | +import time |
| 5 | +import requests |
| 6 | +import hmac |
| 7 | +import hashlib |
| 8 | +from requests_toolbelt.multipart.encoder import MultipartEncoder |
| 9 | + |
| 10 | +AppID = 'Your AppID' |
| 11 | +AppKey = 'Your AppKey' |
| 12 | + |
| 13 | +# doc : https://developer.sunmi.com/docs/zh-CN/cdixeghjk491/faceghjk502 |
| 14 | +url = "https://openapi.sunmi.com/v2/midplat/filecore/file/uploadApk" |
| 15 | +file_path = 'your_apk.apk' |
| 16 | +file_path = 'application.apk' |
| 17 | + |
| 18 | + |
| 19 | +# sign method |
| 20 | +def sign(json_body, timestamp, nonce, appID, appKey): |
| 21 | + message = (json_body + appID + timestamp + nonce).encode('utf-8') # encode to bytes |
| 22 | + print('message=', message) |
| 23 | + return hmac.new(appKey.encode('utf-8'), message, hashlib.sha256).hexdigest() |
| 24 | + |
| 25 | + |
| 26 | +# calculate file md5 |
| 27 | +def calculate_md5(file_path): |
| 28 | + with open(file_path, 'rb') as f: |
| 29 | + data = f.read() |
| 30 | + md5_hash = hashlib.md5(data).hexdigest() |
| 31 | + return md5_hash |
| 32 | + |
| 33 | + |
| 34 | +# get current timestamp |
| 35 | +timestamp = str(int(time.time())) # example: '1700044792' |
| 36 | +nonce = str(random.randint(100000, 999999)) # example: '123456' |
| 37 | +json_body = json.dumps({ |
| 38 | + 'md5': calculate_md5(file_path), |
| 39 | + 'file_type_key': 'appstore_apk', |
| 40 | +}) |
| 41 | + |
| 42 | +sign_str = sign(json_body, timestamp, nonce, AppID, AppKey) |
| 43 | +# sign_str = sign(json.dumps(obj), timestamp, nonce, AppID, AppKey) |
| 44 | +print('sign_str=', sign_str) |
| 45 | + |
| 46 | +# 创建一个MultipartEncoder对象 |
| 47 | +multipart_data = MultipartEncoder( |
| 48 | + fields={ |
| 49 | + 'check': 'no', |
| 50 | + 'params': json_body, |
| 51 | + 'file': (file_path, open(file_path, 'rb'), 'application/octet-stream') |
| 52 | + } |
| 53 | +) |
| 54 | + |
| 55 | +headers = { |
| 56 | + 'User-Agent': 'Python3-Sunmi-Demo', |
| 57 | + 'Content-Type': multipart_data.content_type, |
| 58 | + 'Sunmi-Timestamp': timestamp, |
| 59 | + 'Sunmi-Nonce': nonce, |
| 60 | + 'Sunmi-Appid': AppID, |
| 61 | + 'Sunmi-Sign': sign_str, |
| 62 | +} |
| 63 | + |
| 64 | +response = requests.post(url, data=multipart_data, headers=headers) |
| 65 | +print(response.status_code) |
| 66 | +print(response.json()) |
| 67 | +print('apk_uuid=', response.json()['data']['uuid']) |
0 commit comments