Failed to upload apk via Connect API

Viewed 130

I am working on a python script to update the app on Huawei AppGallery via Connect API. I successfully fetched the token and upload URL but not able to upload the APK/AAB. Getting this error -

{'result': {'CException': {'errorCode': 70001405, 'errorDesc': 'get no file from request!'}, 'resultCode': '70001405'}}

Here's my python script

def uploadAAB(uploadUrl, authCode, accessToken, appId):
    try:
        fileName = 'latest_hms.apk'
        headers = {
            "Authorization": "Bearer " + accessToken,
            "accept": "application/json",
            "client_id": clientId,
            "Content-Type": "multipart/form-data"
        }

        uploadBody = {
            "authCode": authCode,
            "fileCount": 1
        }

        with open(aabPath, 'rb') as f:
            f.seek(0, os.SEEK_END)
            print(f.tell()) # printing the correct size
            first_phase = requests.post(
                uploadUrl,
                files={fileName: f},
                data=uploadBody,
                headers=headers)
            
            if first_phase.status_code == 200:
                print(first_phase.json())
                body = {
                    'fileType': 5,
                    'files': [{
                        'fileName': fileName,
                        'fileDestUrl': first_phase.json()['result']['UploadFileRsp']['fileInfoList'][0]['fileDestUlr'],
                        'size': str(first_phase.json()['result']['UploadFileRsp']['fileInfoList'][0]['size'])
                    }]
                }

                fileHeader = {
                    'client_id': clientId,
                    'Authorization': 'Bearer ' + accessToken,
                }
                params = {
                    'appId': appId,
                }
                second_phase = requests.put(
                    BASE_URL + "/publish/v2/app-file-info",
                    headers=fileHeader,
                    json=body,
                    params=params)
                print(second_phase.json())
        
    except (requests.exceptions.RequestException, requests.exceptions.HTTPError, KeyError) as err:
        stopOnError(repr(err))

Please help me out here.

2 Answers

{'result': {'CException': {'errorCode': 70001405, 'errorDesc': 'get no file from request!'}, 'resultCode': '70001405'}}

This error means there is no file in the request. the file is not include successfully in the request. Please make sure the file is achievable.

It seems Huawei made a change to the AppGallery API in February 2022. I don't know if this was intentional, but you must now specify a filename of "file" instead of your original filename (which worked before). See my pull request on Natgho's HMS-Publishing-API code.

Related