Failed to upload apk via Connect API - huawei-mobile-services

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.

{'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

get token from spotify API using axios, error 404

I`m trying to get the token from the spotify API, I use axios. I use the example given by the API as a guide, but give me the error 404
export const getToken = code => async dispatch => {
const responseToken = await axios.post({
url: "https://accounts.spotify.com/api/token",
form: {
grant_type: "authorization_code",
code,
redirect_uri
},
headers: {
'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
},
json: true
})
console.log(responseToken);
The first line is because I`m using redux,I just wanted you to see that it was a asinc method.
I have being all day trying to fix this, I don`t have more ideas of how to solve this
Try changing
form: {
grant_type: "authorization_code",
code,
redirect_uri
}
to
data: JSON.stringify({
grant_type: "authorization_code",
code,
redirect_uri
})
You want to send it in the request body, hence "data", that's how you define it in axios.
Also, I don't think you need json: true
EDIT:
Pretty sure you have to add 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' to the headers as well.

Trying to run code in IBM Cloud Functions

I have following code I am trying to run in IBM function to get billing data out:
iam_token = 'Bearer eyJraWQiOiIyMDE3MTAzMC0wM****'
def processResourceInstanceUsage(account_id, billMonth):
METERING_HOST = "https://metering-reporting.ng.bluemix.net"
USAGE_URL = "/v4/accounts/"+account_id + \
"/resource_instances/usage/"+billMonth+"?_limit=100&_names=true"
url = METERING_HOST+USAGE_URL
headers = {
"Authorization": "{}".format(iam_token),
"Accept": "application/json",
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
print("\n\nResource instance usage for first 100 items")
return response.json()
processResourceInstanceUsage('*****', '11')
For some reason, I keep on getting 201 unauthorized error. I tried creating iam_token many times. It still gives the same error.
There are few things that should be taken care in the code you provided.
The month you are passing is wrong. It should be in YYYY-MM format.
account_id should be the id next to your Account name when you run ibmcloud target
For IAM token, run this command ibmcloud iam oauth_tokens. If you want to generate access token using your Platform API Key, refer to this link. The word Bearer is not required as this is not an authorization token.
Once you have all this in place, create an IBM Cloud function (Python 3), add the below code, pass the account_id and token and invoke the action to see the result . IBM Cloud function expects a dictionary as an input/parameter and JSON as response
import sys
import requests
def main(dict):
METERING_HOST="https://metering-reporting.ng.bluemix.net"
account_id="3d40d89730XXXXXXX"
billMonth="2018-10"
iam_token="<IAM_TOKEN> or <ACCESS_TOKEN>"
USAGE_URL="/v4/accounts/"+account_id+"/resource_instances/usage/"+billMonth+"?_limit=100&_names=true"
url=METERING_HOST+USAGE_URL
headers = {
"Authorization": "{}".format(iam_token),
"Accept": "application/json",
"Content-Type": "application/json"
}
response=requests.get(url, headers=headers)
print ("\n\nResource instance usage for first 100 items")
return { 'message': response.json() }
This worked for me and returned a JSON with region-wise billing data.
Reference: https://stackoverflow.com/a/52333233/1432067

RC-IamErrorResponse - Account context in the query param is different from the account context in the token

I'm trying to get resource groups via an api key.
First, I authenticate to IAM...
apikey = 'myapikeyvalue'
self.log.debug('Authenticating to IAM')
url = self.iam_endpoint + '/identity/token'
data = "apikey={}&grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey".format(apiKey)
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic Yng6Yng="
}
response = requests.post(url, headers=headers, data=data)
token_type, access_token = # get token_type and access_token from response
Then I receive the account_id ...
url = self.iam_endpoint + '/v1/apikeys/details'
headers = {
"IAM-Apikey": apiKey,
'accept': 'application/json',
'authorization': '{} {}'.format(token_type, access_token),
'cache-control': 'no-cache',
'content-type': 'application/json'
}
response = self._request(url=url, http_method='get', description='_get_account_id', additional_headers=headers)
account_id = response.json()['account_id']
Next, I try to retrieve the resource_groups ...
url = self.region.rc_endpoint() + '/v1/resource_groups?account_id=' + account_id
response = self.client._request(url=url, http_method='get', description='get_resource_groups')
return response.json()
However, this results in:
{
"error_code":"RC-IamErrorResponse",
"message":"Account context in the query param is different from the account context in the token.",
"status_code":401,
"transaction_id":"7e89f6873e1bd4f92d57829e0f08f4ad"
}
Any ideas?
For some reason, returned value seems to be of the format: xxxxxxxxxxxY-YYYY and we only need the x's. This worked for me
return account_id.split('-')[0][:-1]

Can't delete files using a Dropbox Business team token

the following code should work, shouldn't it?
import requests
import json
url = "https://api.dropboxapi.com/2/files/delete_v2"
headers = {
"Authorization": "Bearer <access-token>",
"Content-Type": "application/json",
"Dropbox-Api-Select-Admin":
"dbmid:AADnRVGZHenLtFbLVdHDkqEJg3Dou4hWF4g"
}
data = {"path": "id:Kd_cXYig9pAAAAAAAAAARQ"}
print(requests.post(url, headers=headers, data=json.dumps(data)).content)
returns:
{"error_summary": "path_lookup/not_found/", "error": {".tag": "path_lookup", "path_lookup": {".tag": "not_found"}}}
Tried using Dropbox-Api-Select-User, and delete(v1) and permanently_delete and all failed with the same error.
FYI download worked.
(only with Select-Admin, but worked).
Thanks Greg.
Added "Dropbox-Api-Path-Root" header with the parent_shared_folder_id and the api call worked!
headers = {
"Authorization": "Bearer f7I8uYSFpxAAAAAAAAAFSs-VIz17DnoqmEYXp0MeitxIzXBCYCT4v0Bb4N4_cbxK",
"Content-Type": "application/json",
"Dropbox-Api-Path-Root": "{\".tag\": \"namespace_id\", \"namespace_id\": \"2857852064\"}",
"Dropbox-Api-Select-Admin": "dbmid:AADnRVGZHenLtFbLVdHDkqEJg3Dou4hWF4g"
}

Upload a file with google drive rest api

I am using a service account in google apps script so i can not use the normal google apps script api.
So i use the drive rest api for creating, moving, copy etc. but i am able to upload a file with the rest api. It uploads a file but it is without content or with wrong content.
This is my code:
var contentType = data.substring(5, data.indexOf(';')),
bytes = Utilities.base64Decode(data.substr(data.indexOf('base64,') + 7)),
blob = Utilities.newBlob(bytes, contentType, file);
var service = getService();
if (service.hasAccess()) {
var url = 'https://www.googleapis.com/upload/drive/v3/files?uploadType=media';
var data = {
name: name,
mimeType: "application/pdf",
parents: [parent]
};
var options = {
method: 'post',
contentType: 'application/json',
body: blob ,
payload: JSON.stringify(data),
muteHttpExceptions: true,
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
}
};
var response = UrlFetchApp.fetch(url, options);
var result = JSON.stringify(response.getContentText());
Logger.log(JSON.stringify(result, null, 2));
return result["id"];
} else {
Logger.log(service.getLastError());
}
Thanks
When uploading files using Drive API, you need to use the files.create method. You can see the javascript code reference in Basic uploads where it is used. Also note that the App Script uploading files uses the Drive v2 version of files.insert instead of files.create.