Download file from SharePoint via API using Python

75 Views Asked by At

I am facing 404 error while downloading file from SharePoint via Rest API using Python. Below is my code:

from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext

def get_share_point_context () :
    sharepoint_url = "https://xxxx.sharepoint.com"
    client_id = 'yyyy'
    client_scret = 'zzzz'
    ctx = ClientContext(sharepoint_url).with_client_credentials(client_secret=client_scret,client_id=client_id)
    file_relative_url = "/sites/aaa/Bl_Upload_test/resume.txt"
    file = ctx.web.get_folder_by_server_relative_path(decoded_url=file_relative_url)
    ctx.load(file)
    ctx.execute_query()
    with open('downlaoded_file.txt', 'wb') as f:
        file.download()

get_share_point_context()

Below is the error:

C:\Users\se\PycharmProjects\10135\venv\Scripts\python.exe C:\Users\se\PycharmProjects\10135\sharepoint_file_download.py 
Traceback (most recent call last):
  File "C:\Users\se\PycharmProjects\10135\venv\lib\site-packages\office365\runtime\client_request.py", line 38, in execute_query
    response.raise_for_status()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://xxxx.sharepoint.com/_api/Web/getFolderByServerRelativePath(DecodedUrl='%2Fsites%2Frpa%2FBl_Upload_test%2Fresume.txt')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\se\PycharmProjects\10135\sharepoint_file_download.py", line 16, in <module>
    get_share_point_context()
  File "C:\Users\se\PycharmProjects\10135\sharepoint_file_download.py", line 12, in get_share_point_context
    ctx.execute_query()
  File "C:\Users\se\PycharmProjects\10135\venv\lib\site-packages\office365\runtime\client_runtime_context.py", line 191, in execute_query
    self.pending_request().execute_query(qry)
  File "C:\Users\se\PycharmProjects\10135\venv\lib\site-packages\office365\runtime\client_request.py", line 42, in execute_query
    raise ClientRequestException(*e.args, response=e.response)
office365.runtime.client_request_exception.ClientRequestException: ('-2147024894, System.IO.FileNotFoundException', 'File Not Found.', "404 Client Error: Not Found for url: https://xxxx.sharepoint.com/_api/Web/getFolderByServerRelativePath(DecodedUrl='%2Fsites%2Frpa%2FBl_Upload_test%2Fresume.txt')")

Process finished with exit code 1

but I do have a file resume.txt, and it resides in the site content -> document library named Bl_Upload. I have tried to search many related posts in internet about this problem. Many talks about file naming and special characters, some talks about authentication. So far there is no luck in locating a similar problem. Any help would be appreciated.

0

There are 0 best solutions below