Free Support Forum - aspose.cloud

Protect Word Document using python with Aspose.Words Cloud throws RequestId error

I’m trying to use word document protect api using python.
Kindly help me use the document protection feature. Urgent.

Getting the the following error:

ubuntu@mehulj:~$ python3 /home/ubuntu/Desktop/curl/aspose.py
Traceback (most recent call last):
File “/home/ubuntu/Desktop/curl/aspose.py”, line 27, in
words_api.upload_file(asposewordscloud.models.requests.UploadFileRequest(open(filename, ‘rb’), “”, None))
File “/home/ubuntu/.local/lib/python3.7/site-packages/asposewordscloud/apis/words_api.py”, line 22519, in upload_file
data = self.upload_file_with_http_info(request, **kwargs) # noqa: E501
File “/home/ubuntu/.local/lib/python3.7/site-packages/asposewordscloud/apis/words_api.py”, line 22574, in upload_file_with_http_info
collection_formats=http_params[‘collection_formats’]))
File “/home/ubuntu/.local/lib/python3.7/site-packages/asposewordscloud/api_client.py”, line 400, in call_api
_preload_content, _request_timeout)
File “/home/ubuntu/.local/lib/python3.7/site-packages/asposewordscloud/api_client.py”, line 162, in __call_api
self.request_token()
File “/home/ubuntu/.local/lib/python3.7/site-packages/asposewordscloud/api_client.py”, line 146, in request_token
_request_timeout=None)
File “/home/ubuntu/.local/lib/python3.7/site-packages/asposewordscloud/api_client.py”, line 442, in request
body=body)
File “/home/ubuntu/.local/lib/python3.7/site-packages/asposewordscloud/rest.py”, line 287, in POST
body=body)
File “/home/ubuntu/.local/lib/python3.7/site-packages/asposewordscloud/rest.py”, line 240, in request
raise ApiException(http_resp=r)
File “/home/ubuntu/.local/lib/python3.7/site-packages/asposewordscloud/rest.py”, line 318, in init
self.init_error_response(json.loads(http_resp.data))
File “/home/ubuntu/.local/lib/python3.7/site-packages/asposewordscloud/rest.py”, line 340, in init_error_response
self.body = WordsApiErrorResponse(json_object[“RequestId”], self.init_error(json_object[“Error”]))
KeyError: ‘RequestId’

My Code :
import os

import asposewordscloud

import asposewordscloud.models.requests

from asposewordscloud.apis import WordsApi

import asposewordscloud.models

from asposewordscloud.models import ProtectionRequest

from asposewordscloud.rest import ApiException

import asposewordscloud.rest

client_id=“xyz”

client_secret= “xyz”

words_api = WordsApi(client_id, client_secret)

filename= “/home/ubuntu/Desktop/curl/Words.docx”

body = ProtectionRequest(password=“hello”,new_password=‘123’, protection_type=“AllowOnlyRevisions”)

words_api.upload_file(asposewordscloud.models.requests.UploadFileRequest(open(filename, ‘rb’), “”, None))

@jmehul99

Please check sample working Python code. Hopefully, it will help you to accomplish the task. You may check more unit tests from Aspose.Words Cloud SDK for Python.

# For complete examples and data files, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-python
import os
import asposewordscloud
import asposewordscloud.models.requests
from shutil import copyfile


# Please get your Client ID and Secret from https://dashboard.aspose.cloud.
client_id='xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
client_secret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

words_api = asposewordscloud.WordsApi(client_id,client_secret)
words_api.api_client.configuration.host='https://api.aspose.cloud'


remoteFolder = 'Temp'
localFolder = 'C:/Temp'
localFileName = '02_pages.docx'
remoteFileName = '02_pages.docx'

#upload file
words_api.upload_file(asposewordscloud.models.requests.UploadFileRequest(open(localFolder + '/' + localFileName,'rb'),remoteFolder + '/' + remoteFileName))


#protect document
requestProtectionRequest = asposewordscloud.ProtectionRequest(password='123', protection_type='ReadOnly')
request = asposewordscloud.models.requests.ProtectDocumentRequest(name=remoteFileName, protection_request=requestProtectionRequest, folder=remoteFolder, dest_file_name=remoteFolder + '/' + remoteFileName)

result = words_api.protect_document(request)
print("Result {}".format(result))

#download file
request_download=asposewordscloud.models.requests.DownloadFileRequest(remoteFolder + '/' + remoteFileName)
response_download = words_api.download_file(request_download)
copyfile(response_download, 'C:/Temp/Protected.docx')